相关疑难解决方法(0)

如何在C#中管道Powershell命令

我们的目标是获取Exchange 2010网站的最小数据库,因此我尝试从c#运行以下powershell命令,

Get-MailboxDatabase -server Exchange2010 -Status | select-object Name,DatabaseSize
Run Code Online (Sandbox Code Playgroud)

我苦苦挣扎的问题是-如何通过Select子句命令传递管道。

这是我的尝试

WSManConnectionInfo wsConnectionInfo = new WSManConnectionInfo(new Uri("https://" + ExchangeSite + "/powershell?serializationLevel=Full"),
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange", getCredential());
wsConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
wsConnectionInfo.SkipCACheck = true;
wsConnectionInfo.SkipCNCheck = true;

rsRemoteRunspace = RunspaceFactory.CreateRunspace(wsConnectionInfo);
rsRemoteRunspace.Open();
Pipeline pipeLine = rsRemoteRunspace.CreatePipeline();

Collection<PSObject> DatabaSize = null;

Command myCommand = new Command("Get-MailboxDatabase");
myCommand.Parameters.Add("Server", "Exchange2010");
myCommand.Parameters.Add("Status", null);
Command myCommand2 = new Command("Select-Object");
    myCommand.Parameters.Add("Name");
myCommand.Parameters.Add("DatabaseSize");
pipeLineMB.Commands.Add(myCommand);
pipeLineMB.Commands.Add(myCommand2);
DatabaSize = pipeLine.Invoke();
Run Code Online (Sandbox Code Playgroud)

但我明白了

"A parameter cannot be found that matches parameter name 'Name'."
Run Code Online (Sandbox Code Playgroud)

请记住,我不能使用SnapIn,因为代码必须在运行Exchange服务器上的cmdlet的客户端计算机上运行。

欢迎任何建议。

编辑

我应用了yamen建议的修复程序,并且能够调用该命令,但是当我尝试获取该值时,我得到:“对象引用未设置为对象的实例。” …

.net c# powershell exchange-server

5
推荐指数
1
解决办法
9757
查看次数

标签 统计

.net ×1

c# ×1

exchange-server ×1

powershell ×1