How to run a function in different spaces, PowerShell

I have a powershell script that add registration to the event, but when the event jumps, I get the following error every time the event jumps: “there is no Runspace to run scripts in this thread” I use PS v2, what can I do? Can you provide the code? Thanks

+3
source share
2 answers

Are you on a third-party PowerShell site? This is mistake;)

If you interact with Async callbacks, the problem is that you are working in a different thread, and the [Runspace]::DefaultRunspacethread is static. @ x0n wrote a bridge for Async Callbacks a while ago (I'll see if I can find it).

On the bottom line, you should do what the error suggests: provide one of the DefaultRunspace properties of type System.Management.Automation.Runspaces.Runspace. But you have to do it from the thread where the code will be executed, so it is a little rude with asynchronous callbacks.

[Management.Automation.Runspaces.Runspace]::DefaultRunspace = [RunspaceFactory]::CreateRunspace()
+4
source

Source: https://habr.com/ru/post/1763598/


All Articles