簡體   English   中英

將Powershell命令添加到管道

[英]Add Powershell commands to the pipeline

我正在嘗試從2010 Exchange服務器獲取較低的存儲,該功能將在WCF容器中運行。

我面臨的問題是我無法在管道中運行多個PowerShell命令。

我已經嘗試了以下方法(基於此, 如何通過c#的“ format-list”和“ out-file”管道調用powershell命令? ):

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一個管道),但是我不確定。

聽起來問題出在運行空間。 如果這是一台Exchange服務器,並且您正在Exchange提供的遠程管理會話中運行該服務器,則在該會話中唯一可以做的就是運行Exchange cmdlet。 僅不使用Select-ObjectSort-Object cmdlet以及其他PowerShell語言元素。

考慮到“排序對象”是無法被名為“ http://schemas.microsoft.com/powershell/Microsoft.Exchange ”的架構識別的命令,因此我繼續使用管理單元來開發一個功能,並且該方法工作正常。

注意,我正在使用第一個數據庫,因為默認排序模式是遞增的。 我還要評論一下,如果在Framework 4.0上編譯,您將收到“ Value不能為空錯誤消息”,因此必須更改為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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM