如何在不同的运行空间中运行函数,PowerShell
我有将注册添加到事件的 powershell 脚本,但是当事件跳转时,每次事件跳转时我都会收到以下错误: “没有可用于在此线程中运行脚本的运行空间” 我正在使用 PS v2,我能做什么? 你能提供一些代码吗? 谢谢
I have powershell script that add registration to event, but when the event is jump i'm getting the following error every time the event is jump:
"no Runspace available to run scripts in this thread"
I'm using PS v2, what can i do?
Can you provide some code?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在尝试在非 powershell 线程上运行脚本,可能是通过异步回调。这不是受支持的场景。诀窍是“桥接”回调到事件基础设施,其中脚本块有一个默认的运行空间。看看我在这里写的内容:
https://web.archive.org/web/20190222052659/http://www.nivot.org/blog/post/2009/10/09/PowerShell20AsynchronousCallbacksFromNET
它向您展示了如何在 powershell 中使用异步线程并运行脚本以响应回调。
-奥辛
It sounds like you're trying to run script on a non-powershell thread, probably via an async callback. This is not a supported scenario. The trick is to "bridge" to callback to the eventing infrastructure, where scriptblocks haev a default runspace. Take a look at what I wrote here:
https://web.archive.org/web/20190222052659/http://www.nivot.org/blog/post/2009/10/09/PowerShell20AsynchronousCallbacksFromNET
It shows you how to work with async threads in powershell and run script in response to callbacks.
-Oisin
您是否使用第三方 PowerShell 主机?这是一个错误;)
如果您搞乱了异步回调,问题是您在不同的线程中运行,并且
[Runspace]::DefaultRunspace
是线程静态的。 @x0n 不久前为异步回调写了一个桥(我会看看是否能找到它)。最重要的是,您需要执行错误建议的操作:在 System.Management.Automation.Runspaces.Runspace 类型的 DefaultRunspace 属性中提供一个。但是您必须从要执行代码的线程中执行此操作,因此异步回调有点粗糙。
Are you in a third party PowerShell host? That's a bug ;)
If you're messing around with Async callbacks, the problem is that you're running in a different thread, and the
[Runspace]::DefaultRunspace
is thread static. @x0n wrote a bridge for Async Callbacks awhile back (I'll see if I can find it).Bottom line, you need to do what the error suggests: provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. But you have to do it FROM the thread where the code is going to execute, so it's a bit rough with async callbacks.