How pipe powershell commands in c#

笑着哭i 提交于 2019-12-23 11:54:31

问题


The goal is get the smallest database of an Exchange 2010 site, so I'm trying to run the following powershell command from c#,

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

The problem I'm struggling is - how pipe the Select clause command.

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.

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."

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.


回答1:


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?

Regardless, you might find that the parameter you're actually after is Property viz:

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

And for more than one:

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



回答2:


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.



来源:https://stackoverflow.com/questions/10392901/how-pipe-powershell-commands-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!