Note that there are some explanatory texts on larger screens.

plurals
  1. POHow pipe powershell commands in c#
    primarykey
    data
    text
    <p>The goal is get the smallest database of an Exchange 2010 site, so I'm trying to run the following powershell command from c#,</p> <pre><code>Get-MailboxDatabase -server Exchange2010 -Status | select-object Name,DatabaseSize </code></pre> <p>The problem I'm struggling is - how pipe the Select clause command.</p> <p>This is my attemp, </p> <pre><code>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&lt;PSObject&gt; 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(); </code></pre> <p>but I'm getting, </p> <pre><code>"A parameter cannot be found that matches parameter name 'Name'." </code></pre> <p>Please keep in mind I cannot use SnapIn because the code must run on a client machine that executes cmdlets on the Exchange server.</p> <p>Any advice is welcome. </p> <p><strong>EDIT</strong></p> <p>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."</p> <p><img src="https://i.stack.imgur.com/Sjq06.png" alt="exception"></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload