|
| 1 | +--- |
| 2 | +description: Microsoft.DSC/PowerShell resource reference documentation |
| 3 | +ms.date: 03/03/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: Microsoft/OSInfo |
| 6 | +--- |
| 7 | + |
| 8 | +# Microsoft.DSC/PowerShell |
| 9 | + |
| 10 | +## Synopsis |
| 11 | + |
| 12 | +Adapter for resources implemented as PowerShell classes |
| 13 | + |
| 14 | +## Metadata |
| 15 | + |
| 16 | +```yaml |
| 17 | +Version: 0.1.0 |
| 18 | +Kind: adapter |
| 19 | +Tags: [linux, windows, macos, pwsh, powershell] |
| 20 | +Executable: powershell.resource.ps1 |
| 21 | +``` |
| 22 | +
|
| 23 | +## Instance definition syntax |
| 24 | +
|
| 25 | +```yaml |
| 26 | +resources: |
| 27 | + - name: <instance name> |
| 28 | + type: Microsoft.DSC/PowerShell |
| 29 | + properties: |
| 30 | + # Required Properties |
| 31 | + resources: |
| 32 | + - name: <nested instance name> |
| 33 | + type: <module name>/<class name> |
| 34 | + properties: # adapted resource properties |
| 35 | +``` |
| 36 | +
|
| 37 | +## Description |
| 38 | +
|
| 39 | +The `Microsoft.DSC/PowerShell` adapter resource enables you to use PowerShell Desired State |
| 40 | +Configuration (PSDSC) resources in DSC. The adapter is able to discover and invoke PSDSC resources |
| 41 | +implemented as PowerShell classes. |
| 42 | + |
| 43 | +The adapter manages the PSDSC resources in PowerShell, not Windows PowerShell. To use MOF-based |
| 44 | +PSDSC resources or PSDSC resources that require Windows PowerShell, use the |
| 45 | +[Microsoft.Windows/WindowsPowerShell](../../windows/windowspowershell/resource.md) adapter. |
| 46 | + |
| 47 | +This adapter doesn't use the **PSDesiredStateConfiguration** module. You don't need to install the |
| 48 | +**PSDesiredStateConfiguration** module to use PSDSC resources in DSC through this adapter. |
| 49 | + |
| 50 | +### PowerShell resource adapter cache |
| 51 | + |
| 52 | +The process for discovering the PowerShell resources available to the adapter can be |
| 53 | +time-consuming. To improve performance, the adapter caches PowerShell resources and modules during |
| 54 | +discovery. If the cache doesn't exist during discovery, the adapter creates it. |
| 55 | + |
| 56 | +The location of the cache depends on your operating system. The following table defines the path |
| 57 | +for each platform. |
| 58 | + |
| 59 | +| Platform | Path | |
| 60 | +| :------: | :----------------------------------------| |
| 61 | +| Linux | `$HOME/.dsc/PSAdapterCache.json` | |
| 62 | +| macOS | `$HOME/.dsc/PSAdapterCache.json` | |
| 63 | +| Windows | `%LOCALAPPDATA%\dsc\PSAdapterCache.json` | |
| 64 | + |
| 65 | +The adapter versions the cache. The current version is `1`. If the version of the cache on a |
| 66 | +machine differs from the current version, the adapter refreshes the cache. |
| 67 | + |
| 68 | +The adapter checks whether the cache is stale on each run and refresh it if: |
| 69 | + |
| 70 | +- The `PSModulePath` environmental variable is updated. |
| 71 | +- Any module is added or removed from the `PSModulePath`. |
| 72 | +- Any related file in a cached PSDSC resource module has been updated since the cache was written. |
| 73 | + The adapter watches the `LastWriteTime` property of module files with the following extensions: |
| 74 | + `.ps1`, `.psd1`, and `.psm1`. |
| 75 | + |
| 76 | +You can directly call the adapter script to clear the cache with the **Operation** parameter value |
| 77 | +set to `ClearCache`: |
| 78 | + |
| 79 | +```powershell |
| 80 | +$adapterScript = dsc resource list Microsoft.DSC/PowerShell | |
| 81 | + ConvertFrom-Json | |
| 82 | + Select-Object -ExpandProperty directory | |
| 83 | + Join-Path |
| 84 | +
|
| 85 | +& $adapterScript -Operation CLearCache |
| 86 | +``` |
| 87 | + |
| 88 | +## Requirements |
| 89 | + |
| 90 | +- Using this adapter requires a supported version of PowerShell. DSC invokes the adapter as a |
| 91 | + PowerShell script. For more information about installing PowerShell, see |
| 92 | + [Install PowerShell on Windows, Linux, and macOS](/powershell/scripting/install/installing-powershell). |
| 93 | + |
| 94 | +## Examples |
| 95 | + |
| 96 | +- [Invoke a resource with the PowerShell adapter][02] |
| 97 | +- [Configure a machine with the PowerShell adapter][03] |
| 98 | + |
| 99 | +## Required properties |
| 100 | + |
| 101 | +The following properties are required. |
| 102 | + |
| 103 | +### resources |
| 104 | + |
| 105 | +The `resources` property defines a list of adapted PSDSC resource instances that the adapter manages. |
| 106 | +Every instance in the list must be unique, but instances may share the same DSC resource type. |
| 107 | + |
| 108 | +For more information about defining a valid adapted resource instance, see the |
| 109 | +[Adapted resource instances](#adapted-resource-instances) section of this document. |
| 110 | + |
| 111 | +```yaml |
| 112 | +Type: array |
| 113 | +Required: true |
| 114 | +MinimumItemCount: 1 |
| 115 | +ValidItemSchema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3.0.0/config/document.resource.json |
| 116 | +``` |
| 117 | + |
| 118 | +## Adapted resource instances |
| 119 | + |
| 120 | +Adapted resources instances always adhere to the |
| 121 | +[DSC Configuration document resource instance schema](../../../../schemas/config/resource.md). |
| 122 | + |
| 123 | +Every adapted instance must be an object that defines the [name](#adapted-instance-name), |
| 124 | +[type](#adapted-instance-name), and [properties](#adapted-instance-properties) for the instance. |
| 125 | + |
| 126 | +### Adapted instance name |
| 127 | + |
| 128 | +The `name` property of the adapted resource instance defines the short, human-readable name for the |
| 129 | +instance. The adapted instance name must be a non-empty string containing only letters, numbers, |
| 130 | +and spaces. This property should be unique within the adapter's `resources` array. |
| 131 | + |
| 132 | +> ![NOTE] |
| 133 | +> The adapter doesn't currently raise an error when you define two adapted instances with the same |
| 134 | +> name. In a future release, the adapter will be updated to emit a warning when adapted instances |
| 135 | +> share the same name. In the next major version of the adapter, name conflicts will raise an |
| 136 | +> error. |
| 137 | +> |
| 138 | +> Using the same name for multiple instances can make debugging and reviewing output more |
| 139 | +> difficult. Always use unique names for every instance. |
| 140 | + |
| 141 | +```yaml |
| 142 | +PropertyName: name |
| 143 | +Type: string |
| 144 | +Required: true |
| 145 | +MinimumLength: 1 |
| 146 | +Pattern: ^[a-zA-Z0-9 ]+$ |
| 147 | +``` |
| 148 | + |
| 149 | +### Adapted instance type |
| 150 | + |
| 151 | +The `type` property identifies the adapted instance's PSDSC Resource. The value for this property |
| 152 | +must be the valid fully qualified type name for the resource. |
| 153 | + |
| 154 | +This adapter uses the following syntax for determining the fully qualified type name of a PSDSC |
| 155 | +resource implemented as a PowerShell class: |
| 156 | + |
| 157 | +```Syntax |
| 158 | +<module name>/<class name> |
| 159 | +``` |
| 160 | + |
| 161 | +For example, if a PowerShell module named **TailspinToys** has a class-based PSDSC resource named |
| 162 | +`TSToy`, the fully qualified type name for that resource is `TailspinToys/TSToy`. |
| 163 | + |
| 164 | +For more information about type names in DSC, see |
| 165 | +[DSC Resource fully qualified type name schema reference][01]. |
| 166 | + |
| 167 | +```yaml |
| 168 | +Type: string |
| 169 | +Required: true |
| 170 | +Pattern: ^\w+(\.\w+){0,2}\/\w+$ |
| 171 | +``` |
| 172 | + |
| 173 | +### Adapted instance properties |
| 174 | + |
| 175 | +The `properties` of an adapted resource instance define its desired state. The value of this |
| 176 | +property must be an object. The specified properties are validated at runtime when the adapter |
| 177 | +tries to invoke the adapted PSDSC resource instance. This adapter doesn't support static linting |
| 178 | +for adapted instance properties in a configuration document. |
| 179 | + |
| 180 | +Each name for each property must be a configurable property of the PSDSC resource. The property |
| 181 | +name isn't case sensitive. The value for each property must be valid for that property. If you |
| 182 | +specify an invalid property name or value, the adapter raises an error when it tries to invoke the |
| 183 | +resource. |
| 184 | + |
| 185 | +```yaml |
| 186 | +Type: object |
| 187 | +Required: true |
| 188 | +``` |
| 189 | + |
| 190 | +## Exit Codes |
| 191 | + |
| 192 | +The resource uses the following exit codes to report success and errors: |
| 193 | + |
| 194 | +- `0` - Success |
| 195 | +- `1` - Error |
| 196 | + |
| 197 | +## See also |
| 198 | + |
| 199 | +- [Microsoft.Windows/WindowsPowerShell](../../windows/windowspowershell/resource.md) |
| 200 | + |
| 201 | +<!-- Link references --> |
| 202 | +[01]: ../../../concepts/resources.md#test-operations |
| 203 | +[02]: examples/validate-with-dsc-resource.md |
| 204 | +[03]: examples/validate-in-a-configuration.md |
| 205 | +[04]: cli/osinfo.md |
0 commit comments