-
Notifications
You must be signed in to change notification settings - Fork 49
PSScript resource #937
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
Merged
PSScript resource #937
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5435e59
PSScript resource
6fa58ce
Fixes and tests
28078aa
fix casing
d4981e3
fix information trace and add tracing tests
91bc422
add example config
cc6260e
fix winps manifest, start of support for input
b208f3d
fix build script and handle input to scripts
SteveL-MSFT bd219f2
fix handling of input and add example
SteveL-MSFT f929dde
remove use of out-string
919567d
fix comment in config
b84c52e
move check for errors to before being disposed
9e32bc4
Update resources/PSScript/psscript.tests.ps1
SteveL-MSFT 9994cdf
fix indentation
8007746
switch to register-objectevent and add support for write-host
869d9fb
take Patrick's suggestions
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
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,25 @@ | ||
# Example configuration using PowerShell script resource and using parameters and input | ||
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
parameters: | ||
myName: | ||
type: string | ||
defaultValue: Steve | ||
myObject: | ||
type: object | ||
defaultValue: | ||
color: green | ||
number: 10 | ||
resources: | ||
- name: Use PS script | ||
type: Microsoft.DSC.Transitional/PowerShellScript | ||
properties: | ||
input: | ||
- name: "[parameters('myName')]" | ||
- object: "[parameters('myObject')]" | ||
getScript: | | ||
param($inputArray) | ||
|
||
Write-Warning "This is a warning message" | ||
# any output will be collected and returned | ||
"My name is " + $inputArray[0].name | ||
"My color is " + $inputArray[1].object.color |
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 @@ | ||
./psscript.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,83 @@ | ||
{ | ||
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json", | ||
"type": "Microsoft.DSC.Transitional/PowerShellScript", | ||
"description": "Enable running PowerShell 7 scripts inline", | ||
"version": "0.1.0", | ||
"get": { | ||
"executable": "pwsh", | ||
"args": [ | ||
"-NoLogo", | ||
"-NonInteractive", | ||
"-NoProfile", | ||
"-ExecutionPolicy", | ||
"Bypass", | ||
"-Command", | ||
"$input | ./psscript.ps1", | ||
"get" | ||
], | ||
"input": "stdin" | ||
}, | ||
"set": { | ||
"executable": "pwsh", | ||
"args": [ | ||
"-NoLogo", | ||
"-NonInteractive", | ||
"-NoProfile", | ||
"-ExecutionPolicy", | ||
"Bypass", | ||
"-Command", | ||
"$input | ./psscript.ps1", | ||
"set" | ||
], | ||
"implementsPretest": true, | ||
"input": "stdin", | ||
"return": "state" | ||
}, | ||
"test": { | ||
"executable": "pwsh", | ||
"args": [ | ||
"-NoLogo", | ||
"-NonInteractive", | ||
"-NoProfile", | ||
"-ExecutionPolicy", | ||
"Bypass", | ||
"-Command", | ||
"$input | ./psscript.ps1", | ||
"test" | ||
], | ||
"input": "stdin", | ||
"return": "state" | ||
}, | ||
"exitCodes": { | ||
"0": "Success", | ||
"1": "PowerShell script execution failed", | ||
"2": "PowerShell exception occurred", | ||
"3": "Script had errors" | ||
}, | ||
"schema": { | ||
"embedded": { | ||
"type": "object", | ||
"properties": { | ||
"getScript": { | ||
"type": ["string", "null"] | ||
}, | ||
"setScript": { | ||
"type": ["string", "null"] | ||
}, | ||
"testScript": { | ||
"type": ["string", "null"] | ||
}, | ||
"input": { | ||
"type": ["string", "boolean", "integer", "object", "array", "null"] | ||
}, | ||
"output": { | ||
"type": ["array", "null"] | ||
}, | ||
"_inDesiredState": { | ||
"type": ["boolean", "null"], | ||
"default": null | ||
} | ||
} | ||
} | ||
} | ||
} |
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,177 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(Mandatory = $true, Position = 0)] | ||
[ValidateSet('Get', 'Set', 'Test')] | ||
[string]$Operation, | ||
[Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)] | ||
[string]$jsonInput | ||
) | ||
|
||
function Write-DscTrace { | ||
param( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')] | ||
[string]$Level, | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[string]$Message, | ||
[switch]$Now | ||
) | ||
|
||
$trace = @{$Level.ToLower() = $Message } | ConvertTo-Json -Compress | ||
|
||
if ($Now) { | ||
$host.ui.WriteErrorLine($trace) | ||
} else { | ||
$traceQueue.Enqueue($trace) | ||
} | ||
} | ||
|
||
$scriptObject = $jsonInput | ConvertFrom-Json | ||
|
||
$script = switch ($Operation) { | ||
'Get' { | ||
$scriptObject.GetScript | ||
} | ||
'Set' { | ||
$scriptObject.SetScript | ||
} | ||
'Test' { | ||
$scriptObject.TestScript | ||
} | ||
} | ||
|
||
if ($null -eq $script) { | ||
Write-DscTrace -Now -Level Info -Message "No script found for operation '$Operation'." | ||
if ($Operation -eq 'Test') { | ||
# if not implemented, we return it's in desired state | ||
@{ _inDesiredState = $true } | ConvertTo-Json -Compress | ||
exit 0 | ||
} | ||
|
||
# write an empty json object to stdout | ||
'{}' | ||
exit 0 | ||
} | ||
|
||
# use AST to see if script has param block, if any errors exit with error message | ||
$errors = $null | ||
$tokens = $null | ||
$ast = [System.Management.Automation.Language.Parser]::ParseInput($script, [ref]$tokens, [ref]$errors) | ||
if ($errors.Count -gt 0) { | ||
$errorMessage = $errors | ForEach-Object { $_.ToString() } | ||
Write-DscTrace -Now -Level Error -Message "Script has syntax errors: $errorMessage" | ||
exit 3 | ||
} | ||
|
||
$paramName = if ($null -ne $ast.ParamBlock) { | ||
# make sure it only specifies one parameter and get the name of that parameter | ||
if ($ast.ParamBlock.Parameters.Count -ne 1) { | ||
Write-DscTrace -Now -Level Error -Message 'Script must have exactly one parameter.' | ||
exit 3 | ||
} | ||
$ast.ParamBlock.Parameters[0].Name.VariablePath.UserPath | ||
} else { | ||
$null | ||
} | ||
|
||
$ps = [PowerShell]::Create().AddScript({ | ||
$DebugPreference = 'Continue' | ||
$VerbosePreference = 'Continue' | ||
$ErrorActionPreference = 'Stop' | ||
}).AddStatement().AddScript($script) | ||
|
||
if ($null -ne $scriptObject.input) { | ||
if ($null -eq $paramName) { | ||
Write-DscTrace -Now -Level Error -Message 'Input was provided but script does not have a parameter to accept input.' | ||
exit 3 | ||
} | ||
$null = $ps.AddParameter($paramName, $scriptObject.input) | ||
} elseif ($null -ne $paramName) { | ||
Write-DscTrace -Now -Level Error -Message "Script has a parameter '$paramName' but no input was provided." | ||
exit 3 | ||
} | ||
|
||
$traceQueue = [System.Collections.Concurrent.ConcurrentQueue[object]]::new() | ||
|
||
$null = Register-ObjectEvent -InputObject $ps.Streams.Error -EventName DataAdding -MessageData $traceQueue -Action { | ||
$traceQueue = $Event.MessageData | ||
# convert error to string since it's an ErrorRecord | ||
$traceQueue.Enqueue((@{ error = [string]$EventArgs.ItemAdded } | ConvertTo-Json -Compress)) | ||
} | ||
$null = Register-ObjectEvent -InputObject $ps.Streams.Warning -EventName DataAdding -MessageData $traceQueue -Action { | ||
$traceQueue = $Event.MessageData | ||
$traceQueue.Enqueue((@{ warn = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress)) | ||
} | ||
$null = Register-ObjectEvent -InputObject $ps.Streams.Information -EventName DataAdding -MessageData $traceQueue -Action { | ||
$traceQueue = $Event.MessageData | ||
if ($null -ne $EventArgs.ItemAdded.MessageData) { | ||
if ($EventArgs.ItemAdded.Tags -contains 'PSHOST') { | ||
$traceQueue.Enqueue((@{ info = $EventArgs.ItemAdded.MessageData.ToString() } | ConvertTo-Json -Compress)) | ||
} else { | ||
$traceQueue.Enqueue((@{ trace = $EventArgs.ItemAdded.MessageData.ToString() } | ConvertTo-Json -Compress)) | ||
} | ||
return | ||
} | ||
} | ||
$null = Register-ObjectEvent -InputObject $ps.Streams.Verbose -EventName DataAdding -MessageData $traceQueue -Action { | ||
$traceQueue = $Event.MessageData | ||
$traceQueue.Enqueue((@{ info = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress)) | ||
} | ||
$null = Register-ObjectEvent -InputObject $ps.Streams.Debug -EventName DataAdding -MessageData $traceQueue -Action { | ||
$traceQueue = $Event.MessageData | ||
$traceQueue.Enqueue((@{ debug = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress)) | ||
} | ||
$outputObjects = [System.Collections.Generic.List[Object]]::new() | ||
|
||
function Write-TraceQueue() { | ||
$trace = $null | ||
while (!$traceQueue.IsEmpty) { | ||
if ($traceQueue.TryDequeue([ref] $trace)) { | ||
$host.ui.WriteErrorLine($trace) | ||
} | ||
} | ||
} | ||
|
||
try { | ||
$asyncResult = $ps.BeginInvoke() | ||
while (-not $asyncResult.IsCompleted) { | ||
Write-TraceQueue | ||
|
||
Start-Sleep -Milliseconds 100 | ||
} | ||
$outputCollection = $ps.EndInvoke($asyncResult) | ||
Write-TraceQueue | ||
|
||
|
||
if ($ps.HadErrors) { | ||
# If there are any errors, we will exit with an error code | ||
Write-DscTrace -Now -Level Error -Message 'Errors occurred during script execution.' | ||
exit 1 | ||
SteveL-MSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
foreach ($output in $outputCollection) { | ||
$outputObjects.Add($output) | ||
} | ||
} | ||
catch { | ||
Write-DscTrace -Now -Level Error -Message $_ | ||
exit 1 | ||
SteveL-MSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
finally { | ||
$ps.Dispose() | ||
Get-EventSubscriber | Unregister-Event | ||
} | ||
|
||
# Test should return a single boolean value indicating if in the desired state | ||
if ($Operation -eq 'Test') { | ||
if ($outputObjects.Count -eq 1 -and $outputObjects[0] -is [bool]) { | ||
@{ _inDesiredState = $outputObjects[0] } | ConvertTo-Json -Compress | ||
} else { | ||
Write-DscTrace -Now -Level Error -Message 'Test operation did not return a single boolean value.' | ||
exit 1 | ||
SteveL-MSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} else { | ||
@{ output = $outputObjects } | ConvertTo-Json -Compress -Depth 10 | ||
} |
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.