我通过C#连接到Exchange Online,可以毫无问题地运行Get-Mailbox
命令。但是当我尝试
Get-Mailbox | Where LitigationHoldEnabled -eq $true
我遇到错误
术语“ where.exe”不能识别为cmdlet,函数,脚本文件或可运行程序的名称。检查名称的拼写,或者是否包含路径,请确认路径正确,然后重试。
该命令在PowerShell中运行良好。 我想念什么?
PSCredential credential = new PSCredential("xxxxx@yyyyyyyy.de", secpassword);
string connectionUri = "https://outlook.office365.com/powershell-liveid/";
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace( );
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConnectionUri", new Uri(connectionUri));
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
command.AddParameter("AllowRedirection", true);
powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
Collection<System.Management.Automation.PSObject> result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
powershell.Dispose();
runspace.Dispose();
}
command = new PSCommand();
command.AddCommand("Invoke-Command");
command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Get-Mailbox | Where-Object LitigationHoldEnabled - eq $true"));
command.AddParameter("Session", result[0]);
powershell.Commands = command;
var Searches = powershell.Invoke();
....
powershell.Dispose();
runspace.Dispose();