99 [string ]$jsonInput
1010)
1111
12- $traceQueue = [System.Collections.Queue ]::new()
12+ $traceQueue = [System.Collections.Concurrent.ConcurrentQueue [ object ] ]::new()
1313
1414function Write-DscTrace {
1515 param (
@@ -57,57 +57,90 @@ if ($null -eq $script) {
5757 exit 0
5858}
5959
60+ # use AST to see if script has param block, if any errors exit with error message
61+ $errors = $null
62+ $tokens = $null
63+ $ast = [System.Management.Automation.Language.Parser ]::ParseInput($script , [ref ]$tokens , [ref ]$errors )
64+ if ($errors.Count -gt 0 ) {
65+ $errorMessage = $errors | ForEach-Object { $_.ToString () }
66+ Write-DscTrace - Now - Level Error - Message " Script has syntax errors: $errorMessage "
67+ exit 3
68+ }
69+
70+ $paramName = if ($ast.ParamBlock -ne $null ) {
71+ # make sure it only specifies one parameter and get the name of that parameter
72+ if ($ast.ParamBlock.Parameters.Count -ne 1 ) {
73+ Write-DscTrace - Now - Level Error - Message ' Script must have exactly one parameter.'
74+ exit 3
75+ }
76+ $ast.ParamBlock.Parameters [0 ].Name.VariablePath.UserPath
77+ } else {
78+ $null
79+ }
80+
6081$ps = [PowerShell ]::Create().AddScript({
6182 $DebugPreference = ' Continue'
6283 $VerbosePreference = ' Continue'
6384 $ErrorActionPreference = ' Stop'
64- })
85+ }).AddScript( $script )
6586
6687if ($null -ne $scriptObject.input ) {
67- $null = $ps.AddScript ({
68- $global :inputArg = $scriptObject.input
69- })
88+ if ($null -eq $paramName ) {
89+ Write-DscTrace - Now - Level Error - Message ' Input was provided but script does not have a parameter to accept input.'
90+ exit 3
91+ }
92+ $null = $ps.AddParameter ($paramName , $scriptObject.input )
93+ } elseif ($null -ne $paramName ) {
94+ Write-DscTrace - Now - Level Error - Message " Script has a parameter '$paramName ' but no input was provided."
95+ exit 3
7096}
7197
72- $null = $ps.AddScript ($script )
73-
7498$ps.Streams.Error.add_DataAdded ({
7599 param ($sender , $args )
76- Write-DscTrace - Level Error - Message $sender.Message
100+ Write-DscTrace - Level Error - Message ( $sender.Message | Out-String )
77101})
78102$ps.Streams.Warning.add_DataAdded ({
79103 param ($sender , $args )
80- Write-DscTrace - Level Warn - Message $sender.Message
104+ Write-DscTrace - Level Warn - Message ( $sender.Message | Out-String )
81105})
82106$ps.Streams.Information.add_DataAdded ({
83107 param ($sender , $args )
84- Write-DscTrace - Level Trace - Message $sender.MessageData.ToString ( )
108+ Write-DscTrace - Level Trace - Message ( $sender.MessageData | Out-String )
85109})
86110$ps.Streams.Verbose.add_DataAdded ({
87111 param ($sender , $args )
88- Write-DscTrace - Level Info - Message $sender.Message
112+ Write-DscTrace - Level Info - Message ( $sender.Message | Out-String )
89113})
90114$ps.Streams.Debug.add_DataAdded ({
91115 param ($sender , $args )
92- Write-DscTrace - Level Debug - Message $sender.Message
116+ Write-DscTrace - Level Debug - Message ( $sender.Message | Out-String )
93117})
94118$outputObjects = [System.Collections.Generic.List [Object ]]::new()
95119
120+ function write-traces () {
121+ $trace = $null
122+ while (! $traceQueue.IsEmpty ) {
123+ if ($traceQueue.TryDequeue ([ref ] $trace )) {
124+ $host.ui.WriteErrorLine ($trace )
125+ }
126+ }
127+ }
128+
96129try {
97130 $asyncResult = $ps.BeginInvoke ()
98131 while (-not $asyncResult.IsCompleted ) {
99- While ($traceQueue.Count -gt 0 ) {
100- $trace = $traceQueue.Dequeue ()
101- $host.ui.WriteErrorLine ($trace )
102- }
132+ write-traces
133+ Start-Sleep - Milliseconds 100
103134 }
104135 $outputCollection = $ps.EndInvoke ($asyncResult )
136+ write-traces
137+
105138 foreach ($output in $outputCollection ) {
106139 $outputObjects.Add ($output )
107140 }
108141}
109142catch {
110- Write-DscTrace - Now - Level Error - Message $_.Exception.Message
143+ Write-DscTrace - Now - Level Error - Message ( $_.Exception | Out-String )
111144 exit 1
112145}
113146finally {
@@ -120,11 +153,6 @@ if ($ps.HadErrors) {
120153 exit 1
121154}
122155
123- While ($traceQueue.Count -gt 0 ) {
124- $trace = $traceQueue.Dequeue ()
125- $host.ui.WriteErrorLine ($trace )
126- }
127-
128156# Test should return a single boolean value indicating if in the desired state
129157if ($Operation -eq ' Test' ) {
130158 if ($outputObjects.Count -eq 1 -and $outputObjects [0 ] -is [bool ]) {
0 commit comments