Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#,

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.

like image 374
m0dest0 Avatar asked May 01 '12 02:05

m0dest0


People also ask

How do I pipe a command in PowerShell?

The `|` character in between the two commands is the “pipe” which indicates that instead of displaying the output of the Get-Content command in the PowerShell command window, it should instead pass that data to the next script (the Measure-Object cmdlet).

How does PowerShell piping work?

Powershell pipe works in an asynchronous way. Meaning that output of the first cmdlet is available to the second cmdlet immediately one object at the time (even if the first one has not finished executing). and then stop the execution by pressing Control+C you will see part of directory is written to the text file.

Can you use pipe in PowerShell?

Most PowerShell cmdlets are designed to support pipelines. In most cases, you can pipe the results of a Get cmdlet to another cmdlet of the same noun. For example, you can pipe the output of the Get-Service cmdlet to the Start-Service or Stop-Service cmdlets.


1 Answers

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);
like image 58
yamen Avatar answered Sep 20 '22 22:09

yamen