【问题标题】:Add Powershell commands to the pipeline将 Powershell 命令添加到管道
【发布时间】:2012-03-27 09:37:58
【问题描述】:

我正在尝试从 2010 Exchange 服务器获取较低的存储,该函数将在 WCF 容器中运行。

我面临的问题是我无法在管道中运行多个 PowerShell 命令。

我尝试了以下方法(基于此,how to invoke the powershell command with "format-list" and "out-file" pipeline from c#?):

string strCommand = @"Get-MailboxDatabase -Status | select ServerName,Name,DatabaseSize | Sort-Object DatabaseSize";
string CommandLine = string.Format("&{{{0}}}", strCommand);
pipeLine.Commands.AddScript(CommandLine);

但我明白了:

未处理的异常:System.Management.Automation.RemoteException:在受限语言模式或数据部分中不允许使用脚本块文字。

我也试过了,

Command getMailbox = new Command("Get-MailboxDatabase");
getMailbox.Parameters.Add("Status", null);

Command sort = new Command("Sort-Object");

pipeLine.Commands.Add(getMailbox);
pipeLine.Commands.Add(sort);

Collection<PSObject> commandResults = pipeLine.Invoke();

但不是运气:

未处理的异常:System.Management.Automation.RemoteException:术语“排序对象”未被识别为 cmdlet 的名称

我想知道是否应该使用多个管道(每个 cmdlet 一个管道),但我不确定。

【问题讨论】:

    标签: c# powershell exchange-server-2010


    【解决方案1】:

    听起来问题出在运行空间上。如果这是一台 Exchange 服务器,并且您在 Exchange 提供的远程管理会话中运行它,那么您在该会话中唯一可以做的就是运行 Exchange cmdlet。 Select-Object 和 Sort-Object cmdlet 和其他 PowerShell 语言元素根本无法使用。

    【讨论】:

    • 你的回答帮助我弄清楚 sort-object 是一个我永远无法使用的命令,所以我切换到管理单元实现。谢谢!
    【解决方案2】:

    考虑到 Sort-Object 是一个名为“http://schemas.microsoft.com/powershell/Microsoft.Exchange”的架构无法识别的命令,然后我继续使用管理单元开发一个函数,它工作正常。

    请注意,我正在使用第一个数据库,因为默认排序模式是升序。另外我想评论一下,如果您在 Framework 4.0 上编译,您将收到“Value cannot be null 错误消息”,因此您必须更改为 3.5。

    请记住,它正被 WCF 服务使用,因此管理单元没有问题。如果您想在任何其他应用程序上使用它,例如基于控制台的应用程序,那么您应该在该计算机上安装 EMS 2010。

    这个函数基本上执行下面的PowerShell命令,Get-MailboxDatabase -Status |排序对象数据库大小

        private static string getLowServerStoreDN_SnapIn(string ExchangeSite)
        {
            string strResult = string.Empty;
            RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
            PSSnapInException snapInException = null;
            PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
            Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);
    
            try
            {
                runspace.Open();
                Command getMailbox = new Command("Get-MailboxDatabase");
                getMailbox.Parameters.Add(new CommandParameter("Status", null));
                Command sort = new Command("Sort-Object");
                sort.Parameters.Add("Property", "DatabaseSize");
    
                Pipeline commandPipeLine = runspace.CreatePipeline();
                commandPipeLine.Commands.Add(getMailbox);
                commandPipeLine.Commands.Add(sort);
    
                Collection<PSObject> getmailboxResults = commandPipeLine.Invoke();
    
                if (getmailboxResults.Count > 0)
                {
                    PSObject getMailboxResult = getmailboxResults[0];
                    strResult = getMailboxResult.Properties["Name"].Value.ToString();
                    //foreach (PSObject getMailboxResult in getmailboxResults)
                    //{
                    //    strResult = getMailboxResult.Properties["Name"].Value.ToString();
                    //}
                }
            }
            catch (ApplicationException e)
            {
                //Console.WriteLine(e.Message);
                throw new FaultException("function getLowServerStoreDN_SnapIn(" + ExchangeSite + "): " + e.Message,
                    FaultCode.CreateReceiverFaultCode("BadExchangeServer", "http://example.com"));
            }
            return strResult;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-12
      • 2021-12-17
      • 2021-07-19
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多