@@ -178,7 +178,7 @@ function Get-WebSocket {
178
178
foreach ($key in $keys ) {
179
179
if ($key -isnot [scriptblock ]) {
180
180
throw " Keys '$key ' must be a scriptblock"
181
- }
181
+ }
182
182
}
183
183
foreach ($value in $values ) {
184
184
if ($value -isnot [scriptblock ] -and $value -isnot [string ]) {
@@ -224,13 +224,18 @@ function Get-WebSocket {
224
224
225
225
begin {
226
226
$SocketJob = {
227
- param ([Collections.IDictionary ]$Variable )
227
+ param (
228
+ # By accepting a single parameter containing variables,
229
+ # we can avoid the need to pass in a large number of parameters.
230
+ [Collections.IDictionary ]$Variable
231
+ )
228
232
233
+ # Take every every `-Variable` passed in and define it within the job
229
234
foreach ($keyValue in $variable.GetEnumerator ()) {
230
235
$ExecutionContext.SessionState.PSVariable.Set ($keyValue.Key , $keyValue.Value )
231
236
}
232
237
233
- if ((-not $WebSocketUri ) -or $webSocket ) {
238
+ if ((-not $WebSocketUri )) {
234
239
throw " No WebSocketUri"
235
240
}
236
241
@@ -244,7 +249,7 @@ function Get-WebSocket {
244
249
245
250
$CT = [Threading.CancellationToken ]::None
246
251
247
- if (-not $webSocket ) {
252
+ if ($webSocket -isnot [ Net.WebSockets.ClientWebSocket ] ) {
248
253
$ws = [Net.WebSockets.ClientWebSocket ]::new()
249
254
if ($SubProtocol ) {
250
255
$ws.Options.AddSubProtocol ($SubProtocol )
@@ -328,6 +333,10 @@ function Get-WebSocket {
328
333
foreach ($keyValuePair in $PSBoundParameters.GetEnumerator ()) {
329
334
$Variable [$keyValuePair.Key ] = $keyValuePair.Value
330
335
}
336
+ if ($DebugPreference -notin ' SilentlyContinue' , ' Ignore' ) {
337
+ . $SocketJob - Variable $Variable
338
+ return
339
+ }
331
340
$webSocketJob =
332
341
if ($WebSocketUri ) {
333
342
if (-not $name ) {
@@ -362,32 +371,37 @@ function Get-WebSocket {
362
371
SupportEvent = $true
363
372
}
364
373
$eventSubscriptions = @ (
365
- if ($OnOutput ) {
366
- Register-ObjectEvent @subscriptionSplat - InputObject $webSocketJob.Output - Action $OnOutput
367
- }
368
- if ($OnError ) {
369
- Register-ObjectEvent @subscriptionSplat - InputObject $webSocketJob.Error - Action $OnError
370
- }
371
- if ($OnWarning ) {
372
- Register-ObjectEvent @subscriptionSplat - InputObject $webSocketJob.Warning - Action $OnWarning
373
- }
374
+ if ($webSocketJob ) {
375
+ if ($OnOutput ) {
376
+ Register-ObjectEvent @subscriptionSplat - InputObject $webSocketJob.Output - Action $OnOutput
377
+ }
378
+ if ($OnError ) {
379
+ Register-ObjectEvent @subscriptionSplat - InputObject $webSocketJob.Error - Action $OnError
380
+ }
381
+ if ($OnWarning ) {
382
+ Register-ObjectEvent @subscriptionSplat - InputObject $webSocketJob.Warning - Action $OnWarning
383
+ }
384
+ }
374
385
)
375
386
if ($eventSubscriptions ) {
376
387
$variable [' EventSubscriptions' ] = $eventSubscriptions
377
388
}
378
389
379
- $webSocketConnectTimeout = [DateTime ]::Now + $ConnectionTimeout
380
- while (-not $variable [' WebSocket' ] -and
381
- ([DateTime ]::Now -lt $webSocketConnectTimeout )) {
382
- Start-Sleep - Milliseconds 0
383
- }
390
+ if ($webSocketJob ) {
391
+ $webSocketConnectTimeout = [DateTime ]::Now + $ConnectionTimeout
392
+ while (-not $variable [' WebSocket' ] -and
393
+ ([DateTime ]::Now -lt $webSocketConnectTimeout )) {
394
+ Start-Sleep - Milliseconds 0
395
+ }
396
+
397
+ foreach ($keyValuePair in $Variable.GetEnumerator ()) {
398
+ $webSocketJob.psobject.properties.add (
399
+ [psnoteproperty ]::new($keyValuePair.Key , $keyValuePair.Value ), $true
400
+ )
401
+ }
402
+ $webSocketJob.pstypenames.insert (0 , ' WebSocketJob' )
403
+ }
384
404
385
- foreach ($keyValuePair in $Variable.GetEnumerator ()) {
386
- $webSocketJob.psobject.properties.add (
387
- [psnoteproperty ]::new($keyValuePair.Key , $keyValuePair.Value ), $true
388
- )
389
- }
390
- $webSocketJob.pstypenames.insert (0 , ' WebSocketJob' )
391
405
if ($Watch ) {
392
406
do {
393
407
$webSocketJob | Receive-Job
0 commit comments