了解 PowerShell 托管

发布于 2024-08-24 19:06:54 字数 1053 浏览 1 评论 0原文

我已经完成了一些托管 PowerShell 的工作,并进行了大量阅读,但我看到了奇怪的行为,这让我想知道我是否像我想象的那样理解主机。

我正在使用 RunspaceFactory 创建一个运行空间:

var runSpace = RunspaceFactory.CreateRunspace() 

我在主机的整个执行过程中使用相同的运行空间。当我第一次启动主机时,我调用 Import-Module 命令:

var pipeline = runSpace.CreatePipeline();
var psCommand = new Command("Import-Module");
psCommand.Parameters.Add("Name", directory + "MyModule");
pipeline.Commands.Add(psCommand);
pipeline.Invoke();

“directory”是一个不是默认模块目录的目录。我可以在 PowerShell 命令窗口中使用具有完全相同语法的相同 import-module 命令,并且效果很好。该命令似乎在我的自定义主机中成功完成。稍后在执行过程中,我尝试在模块内调用 cmdlet:

var pipeline = runSpace.CreatePipeline();
var psCommand = new Command("Get-Stuff");
pipeline.Commands.Add(psCommand);
var stuff = pipeline.Invoke();

但是在调用时,我得到了“Get-Stuff”不是 cmdlet 等异常。

我的理解是运行空间会维持这种类型的状态。难道不是这样吗?我已经在另一个项目中成功创建了主机。两个主要区别是该主机使用默认模块目录 (Documents\Modules\),并且我像这样调用 CreateRunspace() 方法:

var runspace = Runspace.CreateRunspace(customHost);

我是否必须定义 PSHost 才能维护状态?

I've done some work hosting PowerShell and have done a lot of reading but I am seeing strange behavior and it makes me wonder if I do not understand the host like I thought I did.

I am creating a Runspace with RunspaceFactory:

var runSpace = RunspaceFactory.CreateRunspace() 

I'm utilizing the same runspace throughout the execution of my host. When I first start the host I invoke a Import-Module command:

var pipeline = runSpace.CreatePipeline();
var psCommand = new Command("Import-Module");
psCommand.Parameters.Add("Name", directory + "MyModule");
pipeline.Commands.Add(psCommand);
pipeline.Invoke();

"directory" is a directory that is not the default module directory. I can use the same import-module command with the exact same syntax in a PowerShell command window and it works fine. The command appears to complete successfully within my custom host. Later in the execution I attempt to call a cmdlet within the module:

var pipeline = runSpace.CreatePipeline();
var psCommand = new Command("Get-Stuff");
pipeline.Commands.Add(psCommand);
var stuff = pipeline.Invoke();

But on invoke I get the exception that "Get-Stuff" is not a cmdlet..etc.

My understanding was the a Runspace would maintain this type of state. Is this not the case? I have created a host successfully within another project. The two main differences are that that host is utilizing the default module directory (Documents\Modules\) and I call the CreateRunspace() method like this:

var runspace = Runspace.CreateRunspace(customHost);

Do I have to define a PSHost to be able to maintain state?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-08-31 19:06:54

这实际上是模块目录尾部带有“\”的问题。

导入模块-名称“D:\aaa\Modules\Xyd\”无效。

This was actually an issue with the directory of the module having a trailing "\".

Import-Module -Name "D:\aaa\Modules\Xyd\" is invalid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文