本文介绍了如何调用在Visual Studio中从C#PoweShell的cmdlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建从Visual Studio中的PowerShell cmdlet和我找不到如何从我的C#文件中调用的cmdlet,或者如果这是甚至可能吗?我有我的运行的cmdlet逐一没有问题,但我想成立一​​个cmdlet来在续集中运行多个cmdlet。

I'm creating a PowerShell cmdlets from Visual Studio and I can't find out how to call cmdlets from within my C# file, or if this is even possible? I have no trouble running my cmdlets one by one, but I want to set up a cmdlet to run multiple cmdlets in a sequel.

感谢

推荐答案

是的,你可以调用从C#的cmdlet 。代码

Yes, you can call cmdlets from your C# code.

您需要这两个命名空间:

You'll need these two namespaces:

using System.Management.Automation;
using System.Management.Automation.Runspaces;



打开运行空间:

Open a runspace:

Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();



创建一个管道:

Create a pipeline:

Pipeline pipeline = runSpace.CreatePipeline();



创建命令:

Create a command:

Command cmd= new Command("APowerShellCommand");

您可以添加参数:

cmd.Parameters.Add("Property", "value");



将它添加到管道:

Add it to the pipeline:

pipeline.Commands.Add(cmd);



运行命令(S):

Run the command(s):

Collection output = pipeline.Invoke();
foreach (PSObject psObject in output)
{
   ....do stuff with psObject (output to console, etc)
}

这是否回答你的问题?

这篇关于如何调用在Visual Studio中从C#PoweShell的cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:32