简体   繁体   English

如何在C#中管道Powershell命令

[英]How pipe powershell commands in c#

The goal is get the smallest database of an Exchange 2010 site, so I'm trying to run the following powershell command from c#, 我们的目标是获取Exchange 2010网站的最小数据库,因此我试图从c#运行以下powershell命令,

Get-MailboxDatabase -server Exchange2010 -Status | select-object Name,DatabaseSize

The problem I'm struggling is - how pipe the Select clause command. 我苦苦挣扎的问题是-如何通过Select子句命令传递管道。

This is my attemp, 这是我的尝试

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();

but I'm getting, 但我明白了

"A parameter cannot be found that matches parameter name 'Name'."

Please keep in mind I cannot use SnapIn because the code must run on a client machine that executes cmdlets on the Exchange server. 请记住,我不能使用SnapIn,因为该代码必须在运行Exchange服务器上的cmdlet的客户端计算机上运行。

Any advice is welcome. 欢迎任何建议。

EDIT 编辑

I applied the fix suggested by yamen and the command was able to be invoked but when I try to get the value, I get: "Object reference not set to an instance of an object." 我应用了yamen建议的修复程序,并且能够调用该命令,但是当我尝试获取该值时,我得到:“对象引用未设置为对象的实例。”

例外

Notice that I can get the values of Servername and Name but it fails in the DatabaseSize so I guess the 'Status' flag is not being set properly because this flag is the one which enables this value. 请注意,我可以获取Servername和Name的值,但是它在DatabaseSize中失败,因此我猜想'Status'标志未正确设置,因为该标志是启用此值的标志。

Here did you mean this: 您的意思是这样的:

Command myCommand2 = new Command("Select-Object");
myCommand2.Parameters.Add("DatabaseSize");

Instead of this: 代替这个:

Command myCommand2 = new Command("Select-Object");
myCommand.Parameters.Add("DatabaseSize");

Notice myCommand2 on the second line? 注意第二行上的myCommand2吗?

Regardless, you might find that the parameter you're actually after is Property viz: 无论如何,您可能会发现实际上要使用的参数是Property viz:

myCommand2.Parameters.Add("Property", "DatabaseSize");

And for more than one: 而且不止一个:

var props = new string[] { "DatabaseSize", "ServerName", "Name" };
myCommand2.Parameters.Add("Property", props);

I just tried this, to have a similar scenario 我只是试过这个,有类似的情况

dir | select Name

It doesn't work. 没用 Gives me the same error saying 'Name' isn't a valid parameter. 给我同样的错误,说“名称”不是有效的参数。 Then I tried the below, it works 然后我尝试了下面的方法

dir | select -first 3

translates to 转换为

        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        Command dir = new Command("dir");
        pipeline.Commands.Add(dir);
        Command select = new Command("select");
        select.Parameters.Add("first", 3);
        pipeline.Commands.Add(select);

You would need to find the name of the parameter for which DatabaseSize is the value, I guess. 我想,您需要找到DatabaseSize是其值的参数的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM