-
Notifications
You must be signed in to change notification settings - Fork 8.1k
DSC v3 resource for Powershell Profile #26157
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adityapatwardhan
merged 20 commits into
PowerShell:master
from
adityapatwardhan:DSCResourceProfile
Nov 13, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b083319
Initial PS Profile resource
adityapatwardhan c74f8bc
dsc ci
adityapatwardhan 8b2b85e
Add experimental feature
adityapatwardhan 522b77d
Add experimental feature for linux
adityapatwardhan d91d6e4
Fixes
adityapatwardhan 9afb9e4
Update boms
adityapatwardhan d55c775
Cleanup profiles properly
adityapatwardhan 492ddb1
Remove unused CI
adityapatwardhan ace8649
Cleanup workflows and actions
adityapatwardhan 9c8ce18
Apply suggestion from @Copilot
adityapatwardhan 350d188
Update test/powershell/dsc/dsc.profileresource.Tests.ps1
adityapatwardhan e38d5b8
Remove -Raw
adityapatwardhan 34fe66c
Remove inadvertent change
adityapatwardhan 2b7e95c
CR feedback from Mikey
adityapatwardhan 3672cd8
Mark empty content test as pending
adityapatwardhan 4104e31
Update descriptions
adityapatwardhan cf207db
Update export test
adityapatwardhan 2555ff3
Ensure v3.2 DSC is installed
adityapatwardhan 6f55a1a
Add error codes in schema
adityapatwardhan 7cec7d0
Address CR feedback from Travis
adityapatwardhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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,126 @@ | ||
| { | ||
| "$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json", | ||
| "description": "Manage PowerShell profiles.", | ||
| "tags": [ | ||
| "Linux", | ||
| "Windows", | ||
| "macOS", | ||
| "PowerShell" | ||
| ], | ||
| "type": "Microsoft.PowerShell/Profile", | ||
| "version": "0.1.0", | ||
| "get": { | ||
| "executable": "pwsh", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-ExecutionPolicy", | ||
| "Bypass", | ||
| "-File", | ||
| "./pwsh.profile.resource.ps1", | ||
| "-operation", | ||
| "get" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "set": { | ||
| "executable": "pwsh", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-ExecutionPolicy", | ||
| "Bypass", | ||
| "-File", | ||
| "./pwsh.profile.resource.ps1", | ||
| "-operation", | ||
| "set" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "export": { | ||
| "executable": "pwsh", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-ExecutionPolicy", | ||
| "Bypass", | ||
| "-File", | ||
| "./pwsh.profile.resource.ps1", | ||
| "-operation", | ||
| "export" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "exitCodes": { | ||
| "0": "Success", | ||
| "1": "Error", | ||
| "2": "Input not supported for export operation" | ||
| }, | ||
| "schema": { | ||
| "embedded": { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "title": "Profile", | ||
| "description": "Manage PowerShell profiles.", | ||
| "type": "object", | ||
| "unevaluatedProperties": false, | ||
| "required": [ | ||
| "profileType" | ||
| ], | ||
| "properties": { | ||
| "profileType": { | ||
| "type": "string", | ||
| "title": "Profile Type", | ||
| "description": "Defines which profile to manage. Valid values are: 'AllUsersCurrentHost', 'AllUsersAllHosts', 'CurrentUserAllHosts', and 'CurrentUserCurrentHost'.", | ||
| "enum": [ | ||
| "AllUsersCurrentHost", | ||
| "AllUsersAllHosts", | ||
| "CurrentUserAllHosts", | ||
| "CurrentUserCurrentHost" | ||
| ] | ||
| }, | ||
| "profilePath": { | ||
| "title": "Profile Path", | ||
| "description": "The full path to the profile file.", | ||
| "type": "string", | ||
| "readOnly": true | ||
| }, | ||
| "content": { | ||
| "title": "Content", | ||
| "description": "Defines the content of the profile. If you don't specify this property, the resource doesn't manage the file contents. If you specify this property as an empty string, the resource removes all content from the file. If you specify this property as a non-empty string, the resource sets the file contents to the specified string. The resources retains newlines from this property without any modification.", | ||
| "type": "string" | ||
| }, | ||
| "_exist": { | ||
| "$ref": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/exist.json" | ||
| }, | ||
| "_name": { | ||
| "$ref": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/name.json" | ||
| } | ||
| }, | ||
| "$defs": { | ||
| "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/exist.json": { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "$id": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/exist.json", | ||
| "title": "Instance should exist", | ||
| "description": "Indicates whether the DSC resource instance should exist.", | ||
| "type": "boolean", | ||
| "default": true, | ||
| "enum": [ | ||
| false, | ||
| true | ||
| ] | ||
| }, | ||
| "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/name.json": { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "$id": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/name.json", | ||
| "title": "Exported instance name", | ||
| "description": "Returns a generated name for the resource instance from an export operation.", | ||
| "readOnly": true, | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,179 @@ | ||
| ## Copyright (c) Microsoft Corporation. All rights reserved. | ||
| ## Licensed under the MIT License. | ||
|
|
||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [ValidateSet('get', 'set', 'export')] | ||
| [string]$Operation, | ||
| [Parameter(ValueFromPipeline)] | ||
| [string[]]$UserInput | ||
| ) | ||
|
|
||
| Begin { | ||
| enum ProfileType { | ||
| AllUsersCurrentHost | ||
| AllUsersAllHosts | ||
| CurrentUserAllHosts | ||
| CurrentUserCurrentHost | ||
| } | ||
|
|
||
| function New-PwshResource { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [ProfileType] $ProfileType, | ||
|
|
||
| [Parameter(ParameterSetName = 'WithContent')] | ||
| [string] $Content, | ||
|
|
||
| [Parameter(ParameterSetName = 'WithContent')] | ||
| [bool] $Exist | ||
| ) | ||
|
|
||
| # Create the PSCustomObject with properties | ||
| $resource = [PSCustomObject]@{ | ||
| profileType = $ProfileType | ||
| content = $null | ||
| profilePath = GetProfilePath -profileType $ProfileType | ||
| _exist = $false | ||
| } | ||
|
|
||
| # Add ToJson method | ||
| $resource | Add-Member -MemberType ScriptMethod -Name 'ToJson' -Value { | ||
| return ([ordered] @{ | ||
| profileType = $this.profileType | ||
| content = $this.content | ||
| profilePath = $this.profilePath | ||
| _exist = $this._exist | ||
| }) | ConvertTo-Json -Compress -EnumsAsStrings | ||
| } | ||
|
|
||
| # Constructor logic - if Content and Exist parameters are provided (WithContent parameter set) | ||
| if ($PSCmdlet.ParameterSetName -eq 'WithContent') { | ||
| $resource.content = $Content | ||
| $resource._exist = $Exist | ||
| } else { | ||
| # Default constructor logic - read from file system | ||
| $fileExists = Test-Path $resource.profilePath | ||
| if ($fileExists) { | ||
| $resource.content = Get-Content -Path $resource.profilePath | ||
| } else { | ||
| $resource.content = $null | ||
| } | ||
| $resource._exist = $fileExists | ||
| } | ||
|
|
||
| return $resource | ||
| } | ||
|
|
||
| function GetProfilePath { | ||
| param ( | ||
| [ProfileType] $profileType | ||
| ) | ||
|
|
||
| $path = switch ($profileType) { | ||
| 'AllUsersCurrentHost' { $PROFILE.AllUsersCurrentHost } | ||
| 'AllUsersAllHosts' { $PROFILE.AllUsersAllHosts } | ||
| 'CurrentUserAllHosts' { $PROFILE.CurrentUserAllHosts } | ||
| 'CurrentUserCurrentHost' { $PROFILE.CurrentUserCurrentHost } | ||
| } | ||
|
|
||
| return $path | ||
| } | ||
|
|
||
| function ExportOperation { | ||
| $allUserCurrentHost = New-PwshResource -ProfileType 'AllUsersCurrentHost' | ||
| $allUsersAllHost = New-PwshResource -ProfileType 'AllUsersAllHosts' | ||
| $currentUserAllHost = New-PwshResource -ProfileType 'CurrentUserAllHosts' | ||
| $currentUserCurrentHost = New-PwshResource -ProfileType 'CurrentUserCurrentHost' | ||
|
|
||
| # Cannot use the ToJson() method here as we are adding a note property | ||
| $allUserCurrentHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'AllUsersCurrentHost' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings | ||
| $allUsersAllHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'AllUsersAllHosts' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings | ||
| $currentUserAllHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'CurrentUserAllHosts' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings | ||
| $currentUserCurrentHost | Add-Member -NotePropertyName '_name' -NotePropertyValue 'CurrentUserCurrentHost' -PassThru | ConvertTo-Json -Compress -EnumsAsStrings | ||
| } | ||
|
|
||
| function GetOperation { | ||
| param ( | ||
| [Parameter(Mandatory = $true)] | ||
| $InputResource, | ||
| [Parameter()] | ||
| [switch] $AsJson | ||
| ) | ||
|
|
||
| $profilePath = GetProfilePath -profileType $InputResource.profileType.ToString() | ||
|
|
||
| $actualState = New-PwshResource -ProfileType $InputResource.profileType | ||
|
|
||
| $actualState.profilePath = $profilePath | ||
|
|
||
| $exists = Test-Path $profilePath | ||
|
|
||
| if ($InputResource._exist -and $exists) { | ||
| $content = Get-Content -Path $profilePath | ||
| $actualState.Content = $content | ||
| } elseif ($InputResource._exist -and -not $exists) { | ||
| $actualState.Content = $null | ||
| $actualState._exist = $false | ||
| } elseif (-not $InputResource._exist -and $exists) { | ||
| $actualState.Content = Get-Content -Path $profilePath | ||
| $actualState._exist = $true | ||
| } else { | ||
| $actualState.Content = $null | ||
| $actualState._exist = $false | ||
| } | ||
|
|
||
| if ($AsJson) { | ||
| return $actualState.ToJson() | ||
| } else { | ||
| return $actualState | ||
| } | ||
| } | ||
|
|
||
| function SetOperation { | ||
| param ( | ||
| $InputResource | ||
| ) | ||
|
|
||
| $actualState = GetOperation -InputResource $InputResource | ||
|
|
||
| if ($InputResource._exist) { | ||
| if (-not $actualState._exist) { | ||
| $null = New-Item -Path $actualState.profilePath -ItemType File -Force | ||
| } | ||
|
|
||
| if ($null -ne $InputResource.content) { | ||
| Set-Content -Path $actualState.profilePath -Value $InputResource.content | ||
| } | ||
| } elseif ($actualState._exist) { | ||
| Remove-Item -Path $actualState.profilePath -Force | ||
| } | ||
| } | ||
| } | ||
| End { | ||
| $inputJson = $input | ConvertFrom-Json | ||
|
|
||
| if ($inputJson) { | ||
| $InputResource = New-PwshResource -ProfileType $inputJson.profileType -Content $inputJson.content -Exist $inputJson._exist | ||
| } | ||
|
|
||
| switch ($Operation) { | ||
| 'get' { | ||
| GetOperation -InputResource $InputResource -AsJson | ||
| } | ||
| 'set' { | ||
| SetOperation -InputResource $InputResource | ||
| } | ||
| 'export' { | ||
| if ($inputJson) { | ||
| Write-Error "Input not supported for export operation" | ||
| exit 2 | ||
| } | ||
|
|
||
| ExportOperation | ||
| } | ||
| } | ||
|
|
||
| exit 0 | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.