-
Notifications
You must be signed in to change notification settings - Fork 8
Add sample for Windows PowerShell (environment variable) #34
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
Open
guillermooo
wants to merge
1
commit into
PowerShell:main
Choose a base branch
from
guillermooo:sample-powershell-env-var
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
samples/powershell/resources/env-variable/env.dsc.resource.json
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,73 @@ | ||
| { | ||
| "$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json", | ||
| "description": "Manages user/machine environment variables in Windows.", | ||
| "type": "Sample.Windows/EnvironmentVariable", | ||
| "version": "0.0.1", | ||
| "get": { | ||
| "executable": "powershell", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-Command", | ||
| "$input | env.resource.ps1 -Operation get" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "set": { | ||
| "executable": "powershell", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-Command", | ||
| "$input | env.resource.ps1 -Operation set" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "delete": { | ||
| "executable": "powershell", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-Command", | ||
| "$input | env.resource.ps1 -Operation delete" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "exitCodes": { | ||
| "0": "Success", | ||
| "1": "Error", | ||
| "2": "Invalid parameters" | ||
| }, | ||
| "schema": { | ||
| "embedded": { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "type": [ | ||
| "object", | ||
| "null" | ||
| ], | ||
| "properties": { | ||
| "name": { | ||
| "type": "string", | ||
| "readOnly": true | ||
| }, | ||
| "value": { | ||
| "type": [ | ||
| "string", | ||
| "null" | ||
| ], | ||
| "readOnly": true | ||
| }, | ||
| "scope": { | ||
| "type": [ | ||
| "string", | ||
| "null" | ||
| ], | ||
| "readOnly": true | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
samples/powershell/resources/env-variable/env.resource.ps1
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,114 @@ | ||
| [CmdletBinding()] | ||
| param( | ||
| [ValidateSet('get', 'set', 'delete')] | ||
| [string]$Operation, | ||
| [Parameter(Mandatory, ValueFromPipeline)] | ||
| [string]$InputObject | ||
| ) | ||
|
|
||
| function Write-DSCTrace | ||
| { | ||
| param( | ||
| [ValidateSet('info', 'warn', 'error')] | ||
| [string]$Level, | ||
| [ValidateNotNullOrEmpty()] | ||
| [string]$Message | ||
| ) | ||
|
|
||
| # We MUST follow a prescriptive format for logs/traces. | ||
| $Host.UI.WriteErrorLine((@{ $Level = $Message } | ConvertTo-Json -Compress)) | ||
| } | ||
|
|
||
| Write-DSCTrace warn "This DSC Resource displays unredacted environment variable values." | ||
|
|
||
| Set-Variable -Option ReadOnly -Name Parameters -Value ($Input | ConvertFrom-Json) | ||
| Set-Variable -Option Constant -Name ExitCodeParameterError -Value 2 | ||
|
|
||
| if ($Parameters.scope -notin ('user', 'machine')) | ||
| { | ||
| Write-DSCTrace error "Invalid parameter 'scope': $($Parameters.scope)." | ||
|
|
||
| # FIXME: Ignored? DSC always returns 1. | ||
| exit $ExitCodeParameterError | ||
| } | ||
|
|
||
| Set-Variable -Option ReadOnly -Name EnvironmentVariableTarget -Value ([System.EnvironmentVariableTarget]$Parameters.scope) | ||
|
|
||
| $result = @{ name = $null ; value = $null ; scope = $null } | ||
|
|
||
| switch ($Operation) | ||
| { | ||
| 'get' | ||
| { | ||
| if ([string]::IsNullOrWhiteSpace($Parameters.name)) | ||
| { | ||
| Write-DSCTrace error "Invalid parameter 'name': $($Parameters.name)." | ||
|
|
||
| exit $ExitCodeParameterError | ||
| } | ||
|
|
||
| $value = [System.Environment]::GetEnvironmentVariable($Parameters.name, $EnvironmentVariableTarget) | ||
|
|
||
| # Assume env var set to null doesn't exist. | ||
| $result['name'] = if ($null -ne $value) { $Parameters.name } else { $null } | ||
| $result['value'] = $value | ||
| $result['scope'] = $EnvironmentVariableTarget.ToString().ToLower() | ||
| } | ||
| 'set' | ||
| { | ||
| if ([string]::IsNullOrWhiteSpace($Parameters.name) -or [string]::IsNullOrEmpty($Parameters.value)) | ||
| { | ||
| Write-DSCTrace error "Invalid value for parameter 'name' ($($Parameters.name)) or parameter 'value' ($($Parameters.value))." | ||
|
|
||
| exit $ExitCodeParameterError | ||
| } | ||
|
|
||
| if ($EnvironmentVariableTarget -eq [System.EnvironmentVariableTarget]::Machine) | ||
| { | ||
| Write-DSCTrace error "The 'set' operation for the scope 'machine' is not implemented." | ||
|
|
||
| exit 1 | ||
| } | ||
|
|
||
| Write-DSCTrace info "Setting environment variable '$($Parameters.name)' to '$($Parameters.value)' ($EnvironmentVariableTarget)." | ||
|
|
||
| [System.Environment]::SetEnvironmentVariable($Parameters.name, $Parameters.value, $EnvironmentVariableTarget) | ||
|
|
||
| $result["name"] = $Parameters.name | ||
| $result["value"] = [System.Environment]::GetEnvironmentVariable($Parameters.name, $EnvironmentVariableTarget) | ||
| $result["scope"] = $EnvironmentVariableTarget.ToString().ToLower() | ||
| } | ||
| 'delete' | ||
| { | ||
| if ([string]::IsNullOrWhiteSpace($Parameters.name)) | ||
| { | ||
| Write-DSCTrace error "Invalid parameter 'name': $($Parameters.name)" | ||
|
|
||
| exit $ExitCodeParameterError | ||
| } | ||
|
|
||
| if ($EnvironmentVariableTarget -eq [System.EnvironmentVariableTarget]::Machine) | ||
| { | ||
| Write-DSCTrace error "The 'delete' operation for the scope 'machine' is not implemented." | ||
|
|
||
| exit 1 | ||
| } | ||
|
|
||
| Write-DSCTrace info "Deleting environment variable '$($Parameters.name)' ($EnvironmentVariableTarget)." | ||
|
|
||
| [System.Environment]::SetEnvironmentVariable($Parameters.name, $null, $EnvironmentVariableTarget) | ||
|
|
||
| $result["name"] = $Parameters.name | ||
| $result["value"] = [System.Environment]::GetEnvironmentVariable($Parameters.name, $EnvironmentVariableTarget) | ||
| $result["scope"] = $EnvironmentVariableTarget.ToString().ToLower() | ||
| } | ||
| default | ||
| { | ||
| Write-DSCTrace error "Operation not implemented: $Operation." | ||
|
|
||
| exit 1 | ||
| } | ||
| } | ||
|
|
||
| return $result | ConvertTo-Json -Compress | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should rather be
$InputObject.