|
| 1 | +--- |
| 2 | +description: Microsoft.DSC.Transitional/PowerShellScript resource reference documentation |
| 3 | +ms.date: 07/07/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: Microsoft.DSC.Transitional/PowerShellScript |
| 6 | +--- |
| 7 | + |
| 8 | +# Microsoft.DSC.Transitional/PowerShellScript |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Enable running PowerShell 7 scripts inline. |
| 13 | + |
| 14 | +> [!IMPORTANT] |
| 15 | +> The `psscript` command and `Microsoft.DSC.Transitional/PowerShellScript` resource |
| 16 | +> is intended as a temporary transitional resource while migrating DSCv3 resources for |
| 17 | +> your needs. |
| 18 | +
|
| 19 | +## Metadata |
| 20 | + |
| 21 | +```yaml |
| 22 | +Version : 0.1.0 |
| 23 | +Kind : resource |
| 24 | +Tags : [Transitional, Windows, Linux, MacOS] |
| 25 | +Author : Microsoft |
| 26 | +``` |
| 27 | +
|
| 28 | +## Instance definition syntax |
| 29 | +
|
| 30 | +```yaml |
| 31 | +resources: |
| 32 | + - name: <instance name> |
| 33 | + type: Microsoft.DSC.Transitional/PowerShellScript |
| 34 | + properties: |
| 35 | + # Optional properties |
| 36 | + getScript: string |
| 37 | + setScript: string |
| 38 | + testScript: string |
| 39 | + input: anyOf # string, boolean, integer, object, array, null |
| 40 | + output: array |
| 41 | + _inDesiredState: boolean # or null |
| 42 | +``` |
| 43 | +
|
| 44 | +## Description |
| 45 | +
|
| 46 | +The `Microsoft.DSC.Transitional/PowerShellScript` resource enables you to run PowerShell 7 scripts inline |
| 47 | +as part of your DSC configuration. This resource is useful for executing PowerShell logic that hasn't |
| 48 | +been fully transitioned to a dedicated DSC resource. |
| 49 | + |
| 50 | +The resource allows you to: |
| 51 | + |
| 52 | +- Define separate PowerShell scripts for get, set, and test operations |
| 53 | +- Pass input data to the scripts |
| 54 | +- Receive output data from the scripts |
| 55 | +- Control the desired state behavior through the `_inDesiredState` property |
| 56 | + |
| 57 | +> [!NOTE] |
| 58 | +> This resource requires PowerShell 7 (`pwsh`) to be installed on the system. |
| 59 | + |
| 60 | +## Capabilities |
| 61 | + |
| 62 | +The resource has the following capabilities: |
| 63 | + |
| 64 | +- `get` - You can use the resource to retrieve the actual state of an instance. |
| 65 | +- `set` - You can use the resource to enforce the desired state for an instance. |
| 66 | +- `test` - You can use the resource to test whether an instance is in the desired state. |
| 67 | + |
| 68 | +This resource implements its own test functionality through the `testScript` property. |
| 69 | +For more information about resource capabilities, see [DSC resource capabilities][00]. |
| 70 | + |
| 71 | +## Examples |
| 72 | + |
| 73 | +1. [Run a simple PowerShell script][01] - Shows how to run a basic PowerShell script. |
| 74 | +2. [PowerShell script with input and output][02] - Shows how to pass data to and from PowerShell scripts. |
| 75 | + |
| 76 | +## Properties |
| 77 | + |
| 78 | +The following list describes the properties for the resource. |
| 79 | + |
| 80 | +- **Instance properties:** <a id="instance-properties"></a> The following properties are optional. |
| 81 | + They define the desired state for an instance of the resource. |
| 82 | + |
| 83 | + - [getScript](#getscript) - The PowerShell script to run during the get operation. |
| 84 | + - [setScript](#setscript) - The PowerShell script to run during the set operation. |
| 85 | + - [testScript](#testscript) - The PowerShell script to run during the test operation. |
| 86 | + - [input](#input) - Input data to pass to the PowerShell scripts. |
| 87 | + - [output](#output) - Output data returned from the PowerShell scripts. |
| 88 | + - [_inDesiredState](#_indesiredstate) - Indicates whether the resource is in the desired state. |
| 89 | + |
| 90 | +### getScript |
| 91 | + |
| 92 | +<details><summary>Expand for <code>getScript</code> property metadata</summary> |
| 93 | + |
| 94 | +```yaml |
| 95 | +Type : string |
| 96 | +IsRequired : false |
| 97 | +IsKey : false |
| 98 | +IsReadOnly : false |
| 99 | +IsWriteOnly : false |
| 100 | +``` |
| 101 | + |
| 102 | +</details> |
| 103 | + |
| 104 | +Defines the PowerShell script to execute during the **Get** operation. This script should return |
| 105 | +the current state of the resource. The script can access input data and should return relevant |
| 106 | +state information. |
| 107 | + |
| 108 | +### setScript |
| 109 | + |
| 110 | +<details><summary>Expand for <code>setScript</code> property metadata</summary> |
| 111 | + |
| 112 | +```yaml |
| 113 | +Type : string |
| 114 | +IsRequired : false |
| 115 | +IsKey : false |
| 116 | +IsReadOnly : false |
| 117 | +IsWriteOnly : false |
| 118 | +``` |
| 119 | + |
| 120 | +</details> |
| 121 | + |
| 122 | +Defines the PowerShell script to execute during the **Set** operation. This script should |
| 123 | +configure the system to match the desired state. The script can access input data and |
| 124 | +should perform the necessary changes to bring the system into compliance. |
| 125 | + |
| 126 | +### testScript |
| 127 | + |
| 128 | +<details><summary>Expand for <code>testScript</code> property metadata</summary> |
| 129 | + |
| 130 | +```yaml |
| 131 | +Type : string |
| 132 | +IsRequired : false |
| 133 | +IsKey : false |
| 134 | +IsReadOnly : false |
| 135 | +IsWriteOnly : false |
| 136 | +``` |
| 137 | + |
| 138 | +</details> |
| 139 | + |
| 140 | +Defines the PowerShell script to execute during the **Test** operation. This script should |
| 141 | +determine whether the system is in the desired state and return appropriate state information. |
| 142 | +The script can access input data and should return state information including the `_inDesiredState` property. |
| 143 | + |
| 144 | +### input |
| 145 | + |
| 146 | +<details><summary>Expand for <code>input</code> property metadata</summary> |
| 147 | + |
| 148 | +```yaml |
| 149 | +Type : anyOf (string, boolean, integer, object, array, null) |
| 150 | +IsRequired : false |
| 151 | +IsKey : false |
| 152 | +IsReadOnly : false |
| 153 | +IsWriteOnly : false |
| 154 | +``` |
| 155 | + |
| 156 | +</details> |
| 157 | + |
| 158 | +Defines input data to pass to the PowerShell scripts. This can be any valid JSON data type |
| 159 | +including strings, booleans, integers, objects, arrays, or null. The input data is available |
| 160 | +to all scripts (get, set, test) and can be used to parameterize script behavior. |
| 161 | + |
| 162 | +### output |
| 163 | + |
| 164 | +<details><summary>Expand for <code>output</code> property metadata</summary> |
| 165 | + |
| 166 | +```yaml |
| 167 | +Type : array |
| 168 | +IsRequired : false |
| 169 | +IsKey : false |
| 170 | +IsReadOnly : false |
| 171 | +IsWriteOnly : false |
| 172 | +``` |
| 173 | + |
| 174 | +</details> |
| 175 | + |
| 176 | +Defines output data returned from the PowerShell scripts. This property contains the results |
| 177 | +of script execution and can include any data that the scripts choose to return. |
| 178 | + |
| 179 | +### _inDesiredState |
| 180 | + |
| 181 | +<details><summary>Expand for <code>_inDesiredState</code> property metadata</summary> |
| 182 | + |
| 183 | +```yaml |
| 184 | +Type : boolean |
| 185 | +IsRequired : false |
| 186 | +IsKey : false |
| 187 | +IsReadOnly : false |
| 188 | +IsWriteOnly : false |
| 189 | +DefaultValue : null |
| 190 | +``` |
| 191 | + |
| 192 | +</details> |
| 193 | + |
| 194 | +Indicates whether the resource is in the desired state. This property is typically set by |
| 195 | +the `testScript` and used by DSC to determine whether the `setScript` needs to be executed. |
| 196 | +When `null` (default), DSC will rely on the test script logic to determine state. |
| 197 | + |
| 198 | +## Instance validating schema |
| 199 | + |
| 200 | +The following snippet contains the JSON Schema that validates an instance of the resource. |
| 201 | + |
| 202 | +```json |
| 203 | +{ |
| 204 | + "type": "object", |
| 205 | + "properties": { |
| 206 | + "getScript": { |
| 207 | + "type": ["string", "null"] |
| 208 | + }, |
| 209 | + "setScript": { |
| 210 | + "type": ["string", "null"] |
| 211 | + }, |
| 212 | + "testScript": { |
| 213 | + "type": ["string", "null"] |
| 214 | + }, |
| 215 | + "input": { |
| 216 | + "type": ["string", "boolean", "integer", "object", "array", "null"] |
| 217 | + }, |
| 218 | + "output": { |
| 219 | + "type": ["array", "null"] |
| 220 | + }, |
| 221 | + "_inDesiredState": { |
| 222 | + "type": ["boolean", "null"], |
| 223 | + "default": null |
| 224 | + } |
| 225 | + }, |
| 226 | + "additionalProperties": false |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +## Exit codes |
| 231 | + |
| 232 | +The resource returns the following exit codes from operations: |
| 233 | + |
| 234 | +- [0](#exit-code-0) - Success |
| 235 | +- [1](#exit-code-1) - PowerShell script execution failed |
| 236 | +- [2](#exit-code-2) - PowerShell exception occurred |
| 237 | +- [3](#exit-code-3) - Script had errors |
| 238 | + |
| 239 | +### Exit code 0 |
| 240 | + |
| 241 | +Indicates the resource operation completed without errors. |
| 242 | + |
| 243 | +### Exit code 1 |
| 244 | + |
| 245 | +Indicates the PowerShell script execution failed. When the resource returns this |
| 246 | +exit code, it also emits an error message with details about the execution failure. |
| 247 | + |
| 248 | +### Exit code 2 |
| 249 | + |
| 250 | +Indicates a PowerShell exception occurred during script execution. When the resource returns this |
| 251 | +exit code, it writes the error to the console. |
| 252 | + |
| 253 | +### Exit code 3 |
| 254 | + |
| 255 | +Indicates the script had errors, typically due to missing or invalid input data. |
| 256 | +This exit code is commonly returned when required input parameters are not provided |
| 257 | +to the PowerShell scripts or when the input data is in an unexpected format. |
| 258 | + |
| 259 | +## See also |
| 260 | + |
| 261 | +- [Microsoft.DSC.Transitional/RunCommandOnSet][03] |
| 262 | +- [Microsoft.DSC.PowerShell][04] |
| 263 | +- [Microsoft.Windows.WindowsPowerShell][05] |
| 264 | + |
| 265 | +<!-- Link definitions --> |
| 266 | +[00]: ../../../../concepts/dsc/resource-capabilities.md |
| 267 | +[01]: ./examples/run-simple-powershell-script.md |
| 268 | +[02]: ./examples/powershell-script-with-input-output.md |
| 269 | +[03]: ../RunCommandOnSet/index.md |
| 270 | +[04]: ../../PowerShell/index.md |
| 271 | +[05]: ../../../../Microsoft/Windows/WindowsPowerShell/index.md |
0 commit comments