- Notifications
You must be signed in to change notification settings - Fork46
Implement export Windows PowerShell adapter#848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
SteveL-MSFT merged 13 commits intoPowerShell:mainfromGijsreyn:implement-export-winpsadapterJun 3, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from5 commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
3955d09
Implement export in WinPS adapter
Gijsreyn5845fcc
Add documentation
Gijsreyn0861dea
Remove test file
Gijsreyn1435850
Added wrong adapter
Gijsreyn390b431
Implement export in WinPS adapter
Gijsreync229ff7
Add documentation
Gijsreynbb4f6c2
Remove test file
Gijsreyn353b76d
Added wrong adapter
Gijsreyna320223
Removed documentation
Gijsreynd7184ea
Merge branch 'implement-export-winpsadapter' of https://github.com/Gi…
Gijsreyn08cb23b
Removed documentation
Gijsreyn617249c
Merge branch 'main' into implement-export-winpsadapter
SteveL-MSFT583f691
Update powershell-adapter/windowspowershell.dsc.resource.json
SteveL-MSFTFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
228 changes: 228 additions & 0 deletions...ources/Microsoft/Windows/WindowsPowerShell/examples/manage-a-windows-service.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
--- | ||
description: > | ||
Examples showing how you can invoke the Microsoft.Windows/WindowsPowerShell with DSC to manage | ||
a Windows service using the PSDesiredStateConfiguration module. | ||
ms.date: 03/25/2025 | ||
ms.topic: reference | ||
title: Manage a Windows service | ||
--- | ||
This example shows how you can use the `Microsoft.Windows/WindowsPowerShell` resource with the `PSDesiredStateConfiguration` module to manage a Windows service. | ||
These examples manage the `Spooler` print spooler service. | ||
> [!NOTE] | ||
> Run this example in an elevated PowerShell session with `dsc.exe` version 3.1.0-preview.2 or later. | ||
## Test whether a service is running | ||
The following snippet shows how you can use the resource with the [dsc resource test][01] command to check whether the `Spooler` service is running. | ||
```powershell | ||
$instance = @{ | ||
Name = 'Spooler' | ||
StartupType = 'Automatic' | ||
} | ConvertTo-Json | ||
dsc resource test --resource PSDesiredStateConfiguration/Service --input $instance | ||
``` | ||
When the service isn't running or has a different startup type, DSC returns the following result: | ||
```yaml | ||
desiredState: | ||
Name: Spooler | ||
StartupType: Manual | ||
actualState: | ||
InDesiredState: false | ||
inDesiredState: false | ||
differingProperties: | ||
- StartupType | ||
``` | ||
The `inDesiredState` field of the result object is set to `false`, indicating that the instance isn't in the desired state. The `differingProperties` field indicates that the `property` property is mismatched between the desired state and actual state. | ||
## Ensure a service is running with automatic startup | ||
To set the system to the desired state and configure the service, use the [dsc resource set][02] command. | ||
```powershell | ||
dsc resource set --resource PSDesiredStateConfiguration/Service --input $instance | ||
``` | ||
When the resource configures the service, DSC returns the following result: | ||
```yaml | ||
beforeState: | ||
Status: null / | ||
Description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. | ||
DisplayName: Print Spooler | ||
ResourceId: null | ||
PsDscRunAsCredential: null | ||
Name: Spooler | ||
Credential: null | ||
PSComputerName: localhost | ||
ConfigurationName: null | ||
Ensure: null | ||
DependsOn: null | ||
SourceInfo: null | ||
BuiltInAccount: LocalSystem | ||
StartupType: Manual | ||
State: Running | ||
ModuleVersion: '1.1' | ||
ModuleName: PSDesiredStateConfiguration | ||
Path: C:\WINDOWS\System32\spoolsv.exe | ||
Dependencies: | ||
- RPCSS | ||
- http | ||
afterState: | ||
Status: null | ||
Description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. | ||
DisplayName: Print Spooler | ||
ResourceId: null | ||
PsDscRunAsCredential: null | ||
Name: Spooler | ||
Credential: null | ||
PSComputerName: localhost | ||
ConfigurationName: null | ||
Ensure: null | ||
DependsOn: null | ||
SourceInfo: null | ||
BuiltInAccount: LocalSystem | ||
StartupType: Automatic | ||
State: Running | ||
ModuleVersion: '1.1' | ||
ModuleName: PSDesiredStateConfiguration | ||
Path: C:\WINDOWS\System32\spoolsv.exe | ||
Dependencies: | ||
- RPCSS | ||
- http | ||
changedProperties: | ||
- StartupType | ||
``` | ||
You can test the instance again to confirm that the service is configured correctly: | ||
```powershell | ||
dsc resource test --resource PSDesiredStateConfiguration/Service --input $instance | ||
``` | ||
```yaml | ||
desiredState: | ||
Name: Spooler / | ||
StartupType: Manual | ||
actualState: | ||
InDesiredState: true | ||
inDesiredState: true | ||
differingProperties: [] | ||
``` | ||
## Stop a service | ||
The following snippet shows how you can configure the `Spooler` service to be stopped with manual startup. | ||
```powershell | ||
$stopInstance = @{ | ||
Name = 'Spooler' | ||
State = 'Stopped' | ||
StartupType = 'Manual' | ||
} | ConvertTo-Json | ||
dsc resource set --resource PSDesiredStateConfiguration/Service --input $stopInstance | ||
``` | ||
When the resource stops the service, DSC returns the following result: | ||
```yaml | ||
beforeState: | ||
Status: null / | ||
Description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. | ||
DisplayName: Print Spooler | ||
ResourceId: null | ||
PsDscRunAsCredential: null | ||
Name: Spooler | ||
Credential: null | ||
PSComputerName: localhost | ||
ConfigurationName: null | ||
Ensure: null | ||
DependsOn: null | ||
SourceInfo: null | ||
BuiltInAccount: LocalSystem | ||
StartupType: Manual | ||
State: Running | ||
ModuleVersion: '1.1' | ||
ModuleName: PSDesiredStateConfiguration | ||
Path: C:\WINDOWS\System32\spoolsv.exe | ||
Dependencies: | ||
- RPCSS | ||
- http | ||
afterState: | ||
Status: null | ||
Description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. | ||
DisplayName: Print Spooler | ||
ResourceId: null | ||
PsDscRunAsCredential: null | ||
Name: Spooler | ||
Credential: null | ||
PSComputerName: localhost | ||
ConfigurationName: null | ||
Ensure: null | ||
DependsOn: null | ||
SourceInfo: null | ||
BuiltInAccount: LocalSystem | ||
StartupType: Manual | ||
State: Stopped | ||
ModuleVersion: '1.1' | ||
ModuleName: PSDesiredStateConfiguration | ||
Path: C:\WINDOWS\System32\spoolsv.exe | ||
Dependencies: | ||
- RPCSS | ||
- http | ||
changedProperties: | ||
- State | ||
``` | ||
## Verify the current state of a service | ||
To check the current state of the service, use the `dsc resource get` command. | ||
```powershell | ||
dsc resource get --resource PSDesiredStateConfiguration/Service --input $instance | ||
``` | ||
```yaml | ||
actualState: | ||
Status: null / | ||
Description: This service spools print jobs and handles interaction with the printer. If you turn off this service, you won't be able to print or see your printers. | ||
DisplayName: Print Spooler | ||
ResourceId: null | ||
PsDscRunAsCredential: null | ||
Name: Spooler | ||
Credential: null | ||
PSComputerName: localhost | ||
ConfigurationName: null | ||
Ensure: null | ||
DependsOn: null | ||
SourceInfo: null | ||
BuiltInAccount: LocalSystem | ||
StartupType: Manual | ||
State: Stopped | ||
ModuleVersion: '1.1' | ||
ModuleName: PSDesiredStateConfiguration | ||
Path: C:\WINDOWS\System32\spoolsv.exe | ||
Dependencies: | ||
- RPCSS | ||
- http | ||
``` | ||
## Restore the original service configuration | ||
If you want to restore the service to its original running state, you can reapply the first configuration. | ||
```powershell | ||
dsc resource set --resource Microsoft.Windows/WindowsPowerShell --input $instance | ||
``` | ||
<!-- Link reference definitions --> | ||
[01]: ../../../../../cli/resource/test.md | ||
[02]: ../../../../../cli/resource/set.md |
133 changes: 133 additions & 0 deletionsdocs/reference/resources/Microsoft/Windows/WindowsPowerShell/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
--- | ||
description: Microsoft.Windows/WindowsPowerShell resource adapter reference documentation | ||
ms.date: 03/25/2025 | ||
ms.topic: reference | ||
title: Microsoft.Windows/WindowsPowerShell | ||
--- | ||
# Microsoft.Windows/WindowsPowerShell | ||
## Synopsis | ||
Manage PowerShell DSC resources. This adapter enables you to use class-based, script-based, or binary PowerShell DSC resources available on the system. | ||
## Metadata | ||
```yaml | ||
Version : 0.1.0 | ||
Kind : resource | ||
Tags : [Windows] | ||
Author : Microsoft | ||
``` | ||
## Instance definition syntax | ||
```yaml | ||
resources: | ||
- name: <instanceName> | ||
type: Microsoft.Windows/WindowsPowerShell | ||
properties: | ||
resources: | ||
- name: <instanceName> | ||
type: <moduleName>/<resourceName> | ||
properties: | ||
# Instance properties | ||
Ensure: Present | ||
# Or from v3.1.0-preview.2 onwards | ||
resources: | ||
- name: <instanceName> | ||
type: <moduleName>/<resourceName> | ||
properties: | ||
# Instance properties | ||
Ensure: Present | ||
``` | ||
## Description | ||
The `Microsoft.Windows/WindowsPowerShell` resource adapter enables you to invoke PSDSC resources. The resource can: | ||
- Execute script-based DSC resources | ||
- Run class-based DSC resource methods | ||
- Execute binary DSC resources | ||
> [!NOTE] | ||
> This resource is installed with DSC itself on Windows systems. | ||
> | ||
> You can update this resource by updating DSC. When you update DSC, the updated version of this | ||
> resource is automatically available. | ||
## Requirements | ||
- The resource is only usable on a Windows system. | ||
- The resource must run in a process context that has appropriate permissions for the DSC resource to be executed. | ||
- The PowerShell modules exposing DSC resources should be installed in | ||
`%PROGRAMFILES%\WindowsPowerShell\Modules` or | ||
`%SystemRoot%\System32\WindowsPowerShell\v1.0\Modules` | ||
## Capabilities | ||
The resource adapter has the following capabilities: | ||
- `get` - You can use the resource to retrieve the actual state of a DSC resource instance. | ||
- `set` - You can use the resource to enforce the desired state for a DSC resource instance. | ||
- `test` - You can use the resource to determine whether a DSC resource instance is in the desired state. | ||
- `export` - You can use the resource to discover and enumerate DSC resource instances currently installed and available on the system. | ||
- `list` - Lists available PowerShell DSC resources on your system that can be used with `dsc.exe`. | ||
> [!NOTE] | ||
> The `export` capability is only available with class-based DSC resources. | ||
> Script-based and binary DSC resources do not support the export operation. | ||
## Examples | ||
1. [Manage a Windows Service][01] - Shows how to manage a Windows service | ||
## Properties | ||
Unlike standard resources, the `Microsoft.Windows/WindowsPowerShell` resource adapter doesn't have directly exposed properties | ||
in its schema because it acts as a bridge to PowerShell DSC resource. Instead, the adapter: | ||
1. Dynamically discovers the property schema for each PowerShell DSC resource | ||
2. Stores the schema properties in a cache file for improved performance in subsequent operations | ||
3. Passes properties to the underlying PowerShell DSC resource | ||
The adapter maintains a cache of resource schemas at: | ||
- Windows: `%LOCALAPPDATA%\dsc\WindowsPSAdapterCache.json` | ||
To list the schema properties for a PowerShell DSC resource, you can run the following command: | ||
```powershell | ||
dsc resource list --adapter Microsoft.Windows/WindowsPowerShell <moduleName>/<resourceName> | | ||
ConvertFrom-Json | | ||
Select-Object properties | ||
``` | ||
You can also retrieve more information by directly reading it from the cache file: | ||
```powershell | ||
$cache = Get-Content -Path "$env:LOCALAPPDATA\dsc\WindowsPSAdapterCache.json" | | ||
ConvertFrom-Json | ||
($cache.ResourceCache | Where-Object -Property type -EQ '<moduleName>/<resourceName>').DscResourceInfo.Properties | ||
``` | ||
## Exit codes | ||
The resource returns the following exit codes from operations: | ||
- [0](#exit-code-0) - Success | ||
- [1](#exit-code-1) - Error | ||
### Exit code 0 | ||
Indicates the resource operation completed without errors. | ||
### Exit code 1 | ||
Indicates the resource operation failed because the underlying DSC resource method or Invoke-DscResource call did not succeed. | ||
When the resource returns this exit code, it also emits an error message with details about the failure. | ||
<!-- Link definitions --> | ||
[01]: ./examples/manage-a-windows-service.md |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.