【发布时间】:2012-05-11 14:15:33
【问题描述】:
我必须列出 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
【问题讨论】:
-
你能分享更多代码吗?
-
@ShayLevy,我添加了代码。谢谢
-
你确定可以使用select-object吗?
-
@ShayLevy,请看我的版本,它使用 Select-Object 工作。
-
更新了我的答案,看看为什么 select-object 可用。
标签: c# .net powershell exchange-server