|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +[CmdletBinding()] |
| 4 | +param( |
| 5 | + [Parameter(Mandatory = $true, Position = 0)] |
| 6 | + [ValidateSet('Get', 'Set', 'Test')] |
| 7 | + [string]$Operation, |
| 8 | + [Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)] |
| 9 | + [string]$jsonInput |
| 10 | +) |
| 11 | + |
| 12 | +function Write-DscTrace { |
| 13 | + param( |
| 14 | + [Parameter(Mandatory = $true)] |
| 15 | + [ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')] |
| 16 | + [string]$Level, |
| 17 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 18 | + [string]$Message, |
| 19 | + [switch]$Now |
| 20 | + ) |
| 21 | + |
| 22 | + $trace = @{$Level.ToLower() = $Message } | ConvertTo-Json -Compress |
| 23 | + |
| 24 | + if ($Now) { |
| 25 | + $host.ui.WriteErrorLine($trace) |
| 26 | + } else { |
| 27 | + $traceQueue.Enqueue($trace) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +$scriptObject = $jsonInput | ConvertFrom-Json |
| 32 | + |
| 33 | +$script = switch ($Operation) { |
| 34 | + 'Get' { |
| 35 | + $scriptObject.GetScript |
| 36 | + } |
| 37 | + 'Set' { |
| 38 | + $scriptObject.SetScript |
| 39 | + } |
| 40 | + 'Test' { |
| 41 | + $scriptObject.TestScript |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +if ($null -eq $script) { |
| 46 | + Write-DscTrace -Now -Level Info -Message "No script found for operation '$Operation'." |
| 47 | + if ($Operation -eq 'Test') { |
| 48 | + # if not implemented, we return it's in desired state |
| 49 | + @{ _inDesiredState = $true } | ConvertTo-Json -Compress |
| 50 | + exit 0 |
| 51 | + } |
| 52 | + |
| 53 | + # write an empty json object to stdout |
| 54 | + '{}' |
| 55 | + exit 0 |
| 56 | +} |
| 57 | + |
| 58 | +# use AST to see if script has param block, if any errors exit with error message |
| 59 | +$errors = $null |
| 60 | +$tokens = $null |
| 61 | +$ast = [System.Management.Automation.Language.Parser]::ParseInput($script, [ref]$tokens, [ref]$errors) |
| 62 | +if ($errors.Count -gt 0) { |
| 63 | + $errorMessage = $errors | ForEach-Object { $_.ToString() } |
| 64 | + Write-DscTrace -Now -Level Error -Message "Script has syntax errors: $errorMessage" |
| 65 | + exit 3 |
| 66 | +} |
| 67 | + |
| 68 | +$paramName = if ($null -ne $ast.ParamBlock) { |
| 69 | + # make sure it only specifies one parameter and get the name of that parameter |
| 70 | + if ($ast.ParamBlock.Parameters.Count -ne 1) { |
| 71 | + Write-DscTrace -Now -Level Error -Message 'Script must have exactly one parameter.' |
| 72 | + exit 3 |
| 73 | + } |
| 74 | + $ast.ParamBlock.Parameters[0].Name.VariablePath.UserPath |
| 75 | +} else { |
| 76 | + $null |
| 77 | +} |
| 78 | + |
| 79 | +$ps = [PowerShell]::Create().AddScript({ |
| 80 | + $DebugPreference = 'Continue' |
| 81 | + $VerbosePreference = 'Continue' |
| 82 | + $ErrorActionPreference = 'Stop' |
| 83 | +}).AddStatement().AddScript($script) |
| 84 | + |
| 85 | +if ($null -ne $scriptObject.input) { |
| 86 | + if ($null -eq $paramName) { |
| 87 | + Write-DscTrace -Now -Level Error -Message 'Input was provided but script does not have a parameter to accept input.' |
| 88 | + exit 3 |
| 89 | + } |
| 90 | + $null = $ps.AddParameter($paramName, $scriptObject.input) |
| 91 | +} elseif ($null -ne $paramName) { |
| 92 | + Write-DscTrace -Now -Level Error -Message "Script has a parameter '$paramName' but no input was provided." |
| 93 | + exit 3 |
| 94 | +} |
| 95 | + |
| 96 | +$traceQueue = [System.Collections.Concurrent.ConcurrentQueue[object]]::new() |
| 97 | + |
| 98 | +$null = Register-ObjectEvent -InputObject $ps.Streams.Error -EventName DataAdding -MessageData $traceQueue -Action { |
| 99 | + $traceQueue = $Event.MessageData |
| 100 | + # convert error to string since it's an ErrorRecord |
| 101 | + $traceQueue.Enqueue((@{ error = [string]$EventArgs.ItemAdded } | ConvertTo-Json -Compress)) |
| 102 | +} |
| 103 | +$null = Register-ObjectEvent -InputObject $ps.Streams.Warning -EventName DataAdding -MessageData $traceQueue -Action { |
| 104 | + $traceQueue = $Event.MessageData |
| 105 | + $traceQueue.Enqueue((@{ warn = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress)) |
| 106 | +} |
| 107 | +$null = Register-ObjectEvent -InputObject $ps.Streams.Information -EventName DataAdding -MessageData $traceQueue -Action { |
| 108 | + $traceQueue = $Event.MessageData |
| 109 | + if ($null -ne $EventArgs.ItemAdded.MessageData) { |
| 110 | + if ($EventArgs.ItemAdded.Tags -contains 'PSHOST') { |
| 111 | + $traceQueue.Enqueue((@{ info = $EventArgs.ItemAdded.MessageData.ToString() } | ConvertTo-Json -Compress)) |
| 112 | + } else { |
| 113 | + $traceQueue.Enqueue((@{ trace = $EventArgs.ItemAdded.MessageData.ToString() } | ConvertTo-Json -Compress)) |
| 114 | + } |
| 115 | + return |
| 116 | + } |
| 117 | +} |
| 118 | +$null = Register-ObjectEvent -InputObject $ps.Streams.Verbose -EventName DataAdding -MessageData $traceQueue -Action { |
| 119 | + $traceQueue = $Event.MessageData |
| 120 | + $traceQueue.Enqueue((@{ info = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress)) |
| 121 | +} |
| 122 | +$null = Register-ObjectEvent -InputObject $ps.Streams.Debug -EventName DataAdding -MessageData $traceQueue -Action { |
| 123 | + $traceQueue = $Event.MessageData |
| 124 | + $traceQueue.Enqueue((@{ debug = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress)) |
| 125 | +} |
| 126 | +$outputObjects = [System.Collections.Generic.List[Object]]::new() |
| 127 | + |
| 128 | +function Write-TraceQueue() { |
| 129 | + $trace = $null |
| 130 | + while (!$traceQueue.IsEmpty) { |
| 131 | + if ($traceQueue.TryDequeue([ref] $trace)) { |
| 132 | + $host.ui.WriteErrorLine($trace) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +try { |
| 138 | + $asyncResult = $ps.BeginInvoke() |
| 139 | + while (-not $asyncResult.IsCompleted) { |
| 140 | + Write-TraceQueue |
| 141 | + |
| 142 | + Start-Sleep -Milliseconds 100 |
| 143 | + } |
| 144 | + $outputCollection = $ps.EndInvoke($asyncResult) |
| 145 | + Write-TraceQueue |
| 146 | + |
| 147 | + |
| 148 | + if ($ps.HadErrors) { |
| 149 | + # If there are any errors, we will exit with an error code |
| 150 | + Write-DscTrace -Now -Level Error -Message 'Errors occurred during script execution.' |
| 151 | + exit 1 |
| 152 | + } |
| 153 | + |
| 154 | + foreach ($output in $outputCollection) { |
| 155 | + $outputObjects.Add($output) |
| 156 | + } |
| 157 | +} |
| 158 | +catch { |
| 159 | + Write-DscTrace -Now -Level Error -Message $_ |
| 160 | + exit 1 |
| 161 | +} |
| 162 | +finally { |
| 163 | + $ps.Dispose() |
| 164 | + Get-EventSubscriber | Unregister-Event |
| 165 | +} |
| 166 | + |
| 167 | +# Test should return a single boolean value indicating if in the desired state |
| 168 | +if ($Operation -eq 'Test') { |
| 169 | + if ($outputObjects.Count -eq 1 -and $outputObjects[0] -is [bool]) { |
| 170 | + @{ _inDesiredState = $outputObjects[0] } | ConvertTo-Json -Compress |
| 171 | + } else { |
| 172 | + Write-DscTrace -Now -Level Error -Message 'Test operation did not return a single boolean value.' |
| 173 | + exit 1 |
| 174 | + } |
| 175 | +} else { |
| 176 | + @{ output = $outputObjects } | ConvertTo-Json -Compress -Depth 10 |
| 177 | +} |
0 commit comments