Skip to content

Commit 8007746

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
switch to register-objectevent and add support for write-host
1 parent 9994cdf commit 8007746

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

resources/PSScript/psscript.ps1

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ param(
99
[string]$jsonInput
1010
)
1111

12-
$traceQueue = [System.Collections.Concurrent.ConcurrentQueue[object]]::new()
13-
1412
function Write-DscTrace {
1513
param(
1614
[Parameter(Mandatory = $true)]
@@ -67,7 +65,7 @@ if ($errors.Count -gt 0) {
6765
exit 3
6866
}
6967

70-
$paramName = if ($ast.ParamBlock -ne $null) {
68+
$paramName = if ($null -ne $ast.ParamBlock) {
7169
# make sure it only specifies one parameter and get the name of that parameter
7270
if ($ast.ParamBlock.Parameters.Count -ne 1) {
7371
Write-DscTrace -Now -Level Error -Message 'Script must have exactly one parameter.'
@@ -82,7 +80,7 @@ $ps = [PowerShell]::Create().AddScript({
8280
$DebugPreference = 'Continue'
8381
$VerbosePreference = 'Continue'
8482
$ErrorActionPreference = 'Stop'
85-
}).AddScript($script)
83+
}).AddStatement().AddScript($script)
8684

8785
if ($null -ne $scriptObject.input) {
8886
if ($null -eq $paramName) {
@@ -95,29 +93,38 @@ if ($null -ne $scriptObject.input) {
9593
exit 3
9694
}
9795

98-
$ps.Streams.Error.add_DataAdded({
99-
param($sender, $args)
100-
Write-DscTrace -Level Error -Message $sender.Message
101-
})
102-
$ps.Streams.Warning.add_DataAdded({
103-
param($sender, $args)
104-
Write-DscTrace -Level Warn -Message $sender.Message
105-
})
106-
$ps.Streams.Information.add_DataAdded({
107-
param($sender, $args)
108-
Write-DscTrace -Level Trace -Message $sender.MessageData.ToString()
109-
})
110-
$ps.Streams.Verbose.add_DataAdded({
111-
param($sender, $args)
112-
Write-DscTrace -Level Info -Message $sender.Message
113-
})
114-
$ps.Streams.Debug.add_DataAdded({
115-
param($sender, $args)
116-
Write-DscTrace -Level Debug -Message $sender.Message
117-
})
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+
$traceQueue.Enqueue((@{ error = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress))
101+
}
102+
$null = Register-ObjectEvent -InputObject $ps.Streams.Warning -EventName DataAdding -MessageData $traceQueue -Action {
103+
$traceQueue = $Event.MessageData
104+
$traceQueue.Enqueue((@{ warn = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress))
105+
}
106+
$null = Register-ObjectEvent -InputObject $ps.Streams.Information -EventName DataAdding -MessageData $traceQueue -Action {
107+
$traceQueue = $Event.MessageData
108+
if ($null -ne $EventArgs.ItemAdded.MessageData) {
109+
if ($EventArgs.ItemAdded.Tags -contains 'PSHOST') {
110+
$traceQueue.Enqueue((@{ info = $EventArgs.ItemAdded.MessageData.ToString() } | ConvertTo-Json -Compress))
111+
} else {
112+
$traceQueue.Enqueue((@{ trace = $EventArgs.ItemAdded.MessageData.ToString() } | ConvertTo-Json -Compress))
113+
}
114+
return
115+
}
116+
}
117+
$null = Register-ObjectEvent -InputObject $ps.Streams.Verbose -EventName DataAdding -MessageData $traceQueue -Action {
118+
$traceQueue = $Event.MessageData
119+
$traceQueue.Enqueue((@{ info = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress))
120+
}
121+
$null = Register-ObjectEvent -InputObject $ps.Streams.Debug -EventName DataAdding -MessageData $traceQueue -Action {
122+
$traceQueue = $Event.MessageData
123+
$traceQueue.Enqueue((@{ debug = $EventArgs.ItemAdded.Message } | ConvertTo-Json -Compress))
124+
}
118125
$outputObjects = [System.Collections.Generic.List[Object]]::new()
119126

120-
function write-traces() {
127+
function Write-TraceQueue() {
121128
$trace = $null
122129
while (!$traceQueue.IsEmpty) {
123130
if ($traceQueue.TryDequeue([ref] $trace)) {
@@ -129,11 +136,13 @@ function write-traces() {
129136
try {
130137
$asyncResult = $ps.BeginInvoke()
131138
while (-not $asyncResult.IsCompleted) {
132-
write-traces
139+
Write-TraceQueue
140+
133141
Start-Sleep -Milliseconds 100
134142
}
135143
$outputCollection = $ps.EndInvoke($asyncResult)
136-
write-traces
144+
Write-TraceQueue
145+
137146

138147
if ($ps.HadErrors) {
139148
# If there are any errors, we will exit with an error code
@@ -151,6 +160,7 @@ catch {
151160
}
152161
finally {
153162
$ps.Dispose()
163+
Get-EventSubscriber | Unregister-Event
154164
}
155165

156166
# Test should return a single boolean value indicating if in the desired state

resources/PSScript/psscript.tests.ps1

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ Describe 'Tests for PSScript resource' {
181181
$yaml = @'
182182
GetScript: |
183183
Write-Warning "This is a warning"
184+
Write-Warning "This is second warning"
184185
'@
185186

186187
$result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
187188
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
188189
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
189-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*This is a warning*'
190+
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*This is a warning*WARN*:*This is second warning*'
190191
}
191192

192193
It 'Write-Error shows up as error traces for <resourceType>' -TestCases $testCases {
@@ -305,4 +306,19 @@ Describe 'Tests for PSScript resource' {
305306
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
306307
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike "*ERROR*:*Script has a parameter 'inputObj' but no input was provided.*"
307308
}
309+
310+
It 'Write-Host results in an info message for <resourceType>' -TestCases $testCases {
311+
param($resourceType)
312+
313+
$yaml = @'
314+
getScript: |
315+
Write-Warning "This is a warning"
316+
Write-Host "This is a host message"
317+
Write-Verbose "This is a verbose message"
318+
'@
319+
$result = dsc -l trace resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
320+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
321+
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
322+
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*This is a warning*INFO*:*This is a host message*INFO*:*This is a verbose message*'
323+
}
308324
}

0 commit comments

Comments
 (0)