9
9
[string ]$jsonInput
10
10
)
11
11
12
- $traceQueue = [System.Collections.Queue ]::new()
12
+ $traceQueue = [System.Collections.Concurrent.ConcurrentQueue [ object ] ]::new()
13
13
14
14
function Write-DscTrace {
15
15
param (
@@ -57,57 +57,90 @@ if ($null -eq $script) {
57
57
exit 0
58
58
}
59
59
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
+
60
81
$ps = [PowerShell ]::Create().AddScript({
61
82
$DebugPreference = ' Continue'
62
83
$VerbosePreference = ' Continue'
63
84
$ErrorActionPreference = ' Stop'
64
- })
85
+ }).AddScript( $script )
65
86
66
87
if ($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
70
96
}
71
97
72
- $null = $ps.AddScript ($script )
73
-
74
98
$ps.Streams.Error.add_DataAdded ({
75
99
param ($sender , $args )
76
- Write-DscTrace - Level Error - Message $sender.Message
100
+ Write-DscTrace - Level Error - Message ( $sender.Message | Out-String )
77
101
})
78
102
$ps.Streams.Warning.add_DataAdded ({
79
103
param ($sender , $args )
80
- Write-DscTrace - Level Warn - Message $sender.Message
104
+ Write-DscTrace - Level Warn - Message ( $sender.Message | Out-String )
81
105
})
82
106
$ps.Streams.Information.add_DataAdded ({
83
107
param ($sender , $args )
84
- Write-DscTrace - Level Trace - Message $sender.MessageData.ToString ( )
108
+ Write-DscTrace - Level Trace - Message ( $sender.MessageData | Out-String )
85
109
})
86
110
$ps.Streams.Verbose.add_DataAdded ({
87
111
param ($sender , $args )
88
- Write-DscTrace - Level Info - Message $sender.Message
112
+ Write-DscTrace - Level Info - Message ( $sender.Message | Out-String )
89
113
})
90
114
$ps.Streams.Debug.add_DataAdded ({
91
115
param ($sender , $args )
92
- Write-DscTrace - Level Debug - Message $sender.Message
116
+ Write-DscTrace - Level Debug - Message ( $sender.Message | Out-String )
93
117
})
94
118
$outputObjects = [System.Collections.Generic.List [Object ]]::new()
95
119
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
+
96
129
try {
97
130
$asyncResult = $ps.BeginInvoke ()
98
131
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
103
134
}
104
135
$outputCollection = $ps.EndInvoke ($asyncResult )
136
+ write-traces
137
+
105
138
foreach ($output in $outputCollection ) {
106
139
$outputObjects.Add ($output )
107
140
}
108
141
}
109
142
catch {
110
- Write-DscTrace - Now - Level Error - Message $_.Exception.Message
143
+ Write-DscTrace - Now - Level Error - Message ( $_.Exception | Out-String )
111
144
exit 1
112
145
}
113
146
finally {
@@ -120,11 +153,6 @@ if ($ps.HadErrors) {
120
153
exit 1
121
154
}
122
155
123
- While ($traceQueue.Count -gt 0 ) {
124
- $trace = $traceQueue.Dequeue ()
125
- $host.ui.WriteErrorLine ($trace )
126
- }
127
-
128
156
# Test should return a single boolean value indicating if in the desired state
129
157
if ($Operation -eq ' Test' ) {
130
158
if ($outputObjects.Count -eq 1 -and $outputObjects [0 ] -is [bool ]) {
0 commit comments