我必须列出 Exchange 2010 服务器中的所有数据库及其邮箱。
在 Powershell 中我可以运行它,
Get-Mailbox -Server Exc2010 | Group-Object Database | Select-Object name,count
Name Count
---- -----
DB01 16
DB04 3
DB02 2
DB03 5
但是,如果我在 C# 中尝试相同的操作,
//Running cmdlets remotely
WSManConnectionInfo wsConnectionInfo = new WSManConnectionInfo(new Uri("https://" + ExchangeSite + "/powershell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", getCredential());
wsConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
wsConnectionInfo.SkipCACheck = true;
wsConnectionInfo.SkipCNCheck = true;
wsConnectionInfo.SkipRevocationCheck = true;
rsRemoteRunspace = RunspaceFactory.CreateRunspace(wsConnectionInfo);
rsRemoteRunspace.Open();
Collection<PSObject> Result = null;
Pipeline pipeLine = rsRemoteRunspace.CreatePipeline();
Command myCommand = new Command("Get-MailboxDatabase");
myCommand.Parameters.Add("Server", "Exc2010");
Command myCommand2 = new Command("Group-Object");
myCommand2.Parameters.Add("Property", "Database");
pipeLine.Commands.Add(myCommand);
pipeLine.Commands.Add(myCommand2);
Result = pipeLine.Invoke();
我明白了,
The term 'Group-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
如果我使用 Select-Object 它可以正常工作,所以我想知道为什么使用 Group-Object 会出现该错误。
任何帮助,将不胜感激。
编辑
如果我使用 Select-Object 运行它,它可以正常工作:
Command myCommand = new Command("Get-MailboxDatabase");
myCommand.Parameters.Add("Server", "Exc2010");
Command myCommand2 = new Command("Select-Object");
myCommand2.Parameters.Add("Property", "Name");
pipeLine.Commands.Add(myCommand);
pipeLine.Commands.Add(myCommand2);
Result = pipeLine.Invoke();
PSObject PSResult = Result.FirstOrDefault();
Console.WriteLine("DB Name: " + PSResult.Properties["Name"].Value.ToString());
我明白了,
DB Name: DB01