99 [string ]$jsonInput
1010)
1111
12- $traceQueue = [System.Collections.Concurrent.ConcurrentQueue [object ]]::new()
13-
1412function 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
8785if ($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() {
129136try {
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}
152161finally {
153162 $ps.Dispose ()
163+ Get-EventSubscriber | Unregister-Event
154164}
155165
156166# Test should return a single boolean value indicating if in the desired state
0 commit comments