【问题标题】:C# and powershell on Exchange 2010 - The term 'Group-Object' is not recognizedExchange 2010 上的 C# 和 powershell - 无法识别术语“组对象”
【发布时间】: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


【解决方案1】:

您是否在 Exchange 提供的远程管理会话中运行此程序?如果是这种情况,您只能运行 Exchange 提供的 cmdlet,而 Group-Object 不是其中之一。

【讨论】:

  • @David,有没有一种支持的命令列表。请看我的上一版,不知道为什么支持Select-Object而不支持Group-Object。
  • 这里是您可以找到它们的地方:technet.microsoft.com/en-us/library/bb124413.aspx
  • @m0dest0 您可以使用以下命令获取可用 cmdlet 的列表:Invoke-Command $session { Get-Command }。令人惊讶的是,Select-Object 在列表中!
【解决方案2】:

@David 是对的。远程端点中只有 Exchange 命令可用。你需要找到另一个解决方案。以下是我在 PowerShell 中的操作方式:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ex1/PowerShell/ -Authentication Basic -Credential (Get-Credential)
Invoke-Command $s { Get-Mailbox } | Group-Object Database -NoElement

请注意,分组发生在邮箱对象到达之后。我不是开发人员,所以我不能告诉你代码在 C# 中的样子。我想您需要创建一个本地运行空间并在其中进行分组。

更新:您可以通过以下方式找到远程会话中可用的 cmdlet:

Invoke-Command $session { Get-Command }

除了 Exchange cmdlet,远程会话中还有 5 个 PowerShell 核心 cmdlet:Exit-PSSessionGet-FormatDataMeasure-ObjectOut-DeafultSelect-Object。这些 cmdlet 是受限端点的一部分。除了它们之外,不存在核心 cmdlet。

【讨论】:

    【解决方案3】:

    我无法运行 group-object,所以我不得不以这种方式更改 cmdlet:

                    Pipeline pipeLine = rsRemoteRunspace.CreatePipeline();
                    Command myCommand = new Command("Get-Mailbox");
                    myCommand.Parameters.Add("Server", Server);
    
                    pipeLine.Commands.Clear();
                    pipeLine.Commands.Add(myCommand);
                    Mailboxes = pipeLine.Invoke();
    
                    foreach (PSObject obj in Mailboxes)
                    {
                        if (mbCount.ContainsKey(obj.Members["Database"].Value.ToString()))
                        {
                            mbCount[obj.Members["Database"].Value.ToString()] += 1;
                        }
                        else
                        {
                            mbCount.Add(obj.Members["Database"].Value.ToString(), 1);
                        }
                    }
                    pipeLine = null;
                }
    
                foreach (String Database in mbCount.Keys)
                {
                    Console.WriteLine("Database : " + Database + " Mailboxes : " + mbCount[Database].ToString());
                }
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 1970-01-01
      • 2021-05-29
      • 2015-07-30
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      相关资源
      最近更新 更多