The was a problem with Exchange in C#.
I need to convert this PowerShell command:
Get-MailboxDatabase -Status | Where-Object {$_.IsExcludedFromProvisioning -eq $false} | select Name,DatabaseSize | sort DatabaseSize
I tried the following code, but it gives an System.Management.Automation.CmdletInvocationException
PSCommand command1 = new PSCommand();
command1.AddCommand("Get-MailboxDatabase");
command1.AddParameter("Status");
command1.AddCommand("Where-Object");
command1.AddArgument("IsExcludedFromProvisioning");
command1.AddParameter("eq", false);
command1.AddCommand("select-object").AddParameter("Property", new string[] { "Name", "DatabaseSize" });
command1.AddCommand("sort-object").AddParameter("Property", "DatabaseSize");
PowershellExchange.Commands = command1;
PowershellExchange.Runspace = RunspaceExchange;
var tmp1 = PowershellExchange.Invoke();
Tell me, what could be the problem, or how can you alternatively rewrite the powershell command?
Edit
Inner Exception: The specified operator requires both the -Property parameter and the -Value parameter. Specify values for both parameters and run the command again.
This may seem counter-intuitive, but in the following:
... |Where-Object Something -eq $SomeValue
-eq
is a actually a switch parameter, and the verbose form of this would be:
... |Where-Object -Property:Something -Eq:$true -Value:$SomeValue
So, to re-create the Where-Object
pipeline element without a filter scriptblock:
command1.AddCommand("Where-Object")
.AddParameter("Property", "IsExcludedFromProvisioning")
.AddParameter("Eq", true)
.AddParameter("Value", false);
Thanks @Jeroen Mostert for help
Final code looks like
PSCommand command1 = new PSCommand();
command1.AddScript("Get-MailboxDatabase -Status | Where-Object {$_.IsExcludedFromProvisioning -eq $false} | select Name,DatabaseSize | sort DatabaseSize");
PowershellExchange.Commands = command1;
PowershellExchange.Runspace = RunspaceExchange;
var tmp1 = PowershellExchange.Invoke();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.