Powershell命令C#中的参数 [英] Powershell Commands Parameters in C#

查看:70
本文介绍了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();

推荐答案

您好,

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

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


pipeLine.Commands.Add(myCommand);


这篇关于Powershell命令C#中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆