简体   繁体   English

如何在不同的运行空间中运行功能,PowerShell

[英]How to run function in different runspace, PowerShell

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? 我有为事件添加注册的Powershell脚本,但是当事件跳转时,每次事件跳转时我都会收到以下错误:“没有可用于在该线程中运行脚本的运行空间”我正在使用PS v2,可以我做? Can you provide some code? 你能提供一些代码吗? Thanks 谢谢

Are you in a third party PowerShell host? 您是否在第三方PowerShell主机中? 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. 如果您正在使用Async回调,那么问题是您正在其他线程中运行,并且[Runspace]::DefaultRunspace是线程静态的。 @x0n wrote a bridge for Async Callbacks awhile back (I'll see if I can find it). @ x0n不久前为异步回调写了一个桥(我看看是否能找到它)。

Bottom line, you need to do what the error suggests: provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. 最重要的是,您需要执行错误所建议的操作:在System.Management.Automation.Runspaces.Runspace类型的DefaultRunspace属性中提供一个。 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. 但是您必须在要执行代码的线程中执行此操作,因此异步回调的操作有点麻烦。

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

It sounds like you're trying to run script on a non-powershell thread, probably via an async callback. 听起来您正在尝试通过非异步回调在非Powershell线程上运行脚本。 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 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. 它向您展示了如何在Powershell中使用异步线程并如何运行脚本以响应回调。

-Oisin -Oisin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM