Powershell命令C#中的参数

问题描述:

我能够在我的C#应用​​程序中创建一个管道并执行powershell命令,但是当我尝试做一些更具体的操作时,我会收到一个错误,指出函数或cmdlet不存在。

I was able to create a pipeline and execute powershell commands from within my C# application, however when I try to do something more specific I get an error that says the function or cmdlet doesnt exist.

让我举个例子,我可以运行Get-Mailbox,但是当我执行Get-Mailbox -ServerName nameOfServer时,我在这些情况下会出错。我假设我需要将这些值设置为某种参数但是我无法弄清楚这一点。

Let me give you an example, I can run Get-Mailbox, but when I do Get-Mailbox -ServerName nameOfServer, I get an error in these cases. I assume I need to set these values as some sort of parameter however I have not been able to figure this out.

有人知道如何设置这些参数吗?

Does anybody know how I can set these parameters?

这是我的代码


      RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
      PSSnapInException snapInException = null;
      rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
      rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.Powershell.Support", out snapInException);
      Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
      myRunSpace.Open();
      Pipeline pipeLine = myRunSpace.CreatePipeline();
      string GetMailboxCommand = "Get-Mailbox";
      Command myCommand = new Command(GetMailboxCommand.ToString());
      pipeLine.Commands.Add(myCommand);
      Collection<PSObject> commandResults = pipeLine.Invoke();

您好,

您必须添加参数" ;服务器&QUOT;在这一行之后:

You have to add parameter "ServerName" after this line:


pipeLine.Commands.Add(myCommand);