9
9
[string ]$jsonInput
10
10
)
11
11
12
- $traceQueue = [System.Collections.Concurrent.ConcurrentQueue [object ]]::new()
13
-
14
12
function Write-DscTrace {
15
13
param (
16
14
[Parameter (Mandatory = $true )]
@@ -67,7 +65,7 @@ if ($errors.Count -gt 0) {
67
65
exit 3
68
66
}
69
67
70
- $paramName = if ($ast .ParamBlock -ne $null ) {
68
+ $paramName = if ($null -ne $ast .ParamBlock ) {
71
69
# make sure it only specifies one parameter and get the name of that parameter
72
70
if ($ast.ParamBlock.Parameters.Count -ne 1 ) {
73
71
Write-DscTrace - Now - Level Error - Message ' Script must have exactly one parameter.'
@@ -82,7 +80,7 @@ $ps = [PowerShell]::Create().AddScript({
82
80
$DebugPreference = ' Continue'
83
81
$VerbosePreference = ' Continue'
84
82
$ErrorActionPreference = ' Stop'
85
- }).AddScript($script )
83
+ }).AddStatement(). AddScript($script )
86
84
87
85
if ($null -ne $scriptObject.input ) {
88
86
if ($null -eq $paramName ) {
@@ -95,29 +93,38 @@ if ($null -ne $scriptObject.input) {
95
93
exit 3
96
94
}
97
95
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
+ }
118
125
$outputObjects = [System.Collections.Generic.List [Object ]]::new()
119
126
120
- function write-traces () {
127
+ function Write-TraceQueue () {
121
128
$trace = $null
122
129
while (! $traceQueue.IsEmpty ) {
123
130
if ($traceQueue.TryDequeue ([ref ] $trace )) {
@@ -129,11 +136,13 @@ function write-traces() {
129
136
try {
130
137
$asyncResult = $ps.BeginInvoke ()
131
138
while (-not $asyncResult.IsCompleted ) {
132
- write-traces
139
+ Write-TraceQueue
140
+
133
141
Start-Sleep - Milliseconds 100
134
142
}
135
143
$outputCollection = $ps.EndInvoke ($asyncResult )
136
- write-traces
144
+ Write-TraceQueue
145
+
137
146
138
147
if ($ps.HadErrors ) {
139
148
# If there are any errors, we will exit with an error code
@@ -151,6 +160,7 @@ catch {
151
160
}
152
161
finally {
153
162
$ps.Dispose ()
163
+ Get-EventSubscriber | Unregister-Event
154
164
}
155
165
156
166
# Test should return a single boolean value indicating if in the desired state
0 commit comments