-
Notifications
You must be signed in to change notification settings - Fork 87
Known issues with runspaces
Some built-in types have additional ScriptProperty objects added by PowerShell. A good example is the [guid] type which has a Guid ScriptProperty of {$this.ToString()}. Normally you could define a function parameter in the param() block like:
$MyParameter = [guid]::NewGuid().Guid,
However under heavy concurrent load it appears that there is a very small delay to initialising ScriptProperty methods such as this, which will result in random BeginInvoke errors in PoshRSJob.
Excellent workarounds are to directly access the underlying object property rather than going through a ScriptProperty (i.e. using .ToString()).
$MyParameter = [guid]::NewGuid().ToString()
You could also defer that initialisation until your begin {} block. This is only a problem in the parameter definition for functions and never anywhere else.
Functions which have SupportsShouldProcess will work as normal however the What If: output which would normally be output to screen by $PSCmdlet.ShouldProcess() outputs nothing in an RSJob. This is because it's not written to a normal stream and is instead handled in a special way by PowerShell with no known way to interact with it.
As a workaround you could use your own function that outputs to normal streams, or, potentially force override $PSCmdlet.ShouldProcess with your own wrapper ScriptMethod (though this would be cumbersome).