How pipe powershell commands in c#

11,626

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

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

Share:
11,626
m0dest0
Author by

m0dest0

Updated on June 24, 2022

Comments

  • m0dest0
    m0dest0 almost 2 years

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

    exception

    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.

  • yamen
    yamen almost 12 years
    As per my answer, that parameter is called 'Property'.
  • m0dest0
    m0dest0 almost 12 years
    I was able to invoked the command but now I get an error when try to get the value. I guess the -Status flag is not being applied properly, can you confirm please if I'm using it in the proper way. See my comments above.
  • yamen
    yamen almost 12 years
    Try either myCommand.Parameters.Add("Status", true); or just myCommand.Parameters.Add("Status"); and report back.
  • m0dest0
    m0dest0 almost 12 years
    I just realized that the flag must be entered in the followig way, myCommand.Parameters.Add("Status", true); Thank you.
  • m0dest0
    m0dest0 almost 12 years
    yamen, unmarked the answer just to caught your attention, hopefully you get a notification and can comment on this thread. Thanks
  • yamen
    yamen almost 12 years
    @m0dest0 Can I suggest you open the new one as another question? It's going to be a different problem, and you're not really meant to use one question that you continually edit for different problems. People will be unlikely to answer your questions if they solve the problem and then you give another one and take the rep away.
  • yamen
    yamen almost 12 years
    Also note you can just type @yamen in a comment to page me. Do so in the new question and I'll take a look.