简体   繁体   English

将Powershell命令添加到管道

[英]Add Powershell commands to the pipeline

I'm trying to get the lower store from a 2010 Exchange server, and the function will run in a WCF container. 我正在尝试从2010 Exchange服务器获取较低的存储,该功能将在WCF容器中运行。

The problem I'm facing is that I'm unable to run multiple PowerShell commands in the pipeline. 我面临的问题是我无法在管道中运行多个PowerShell命令。

I've tried the following (based on this, how to invoke the powershell command with "format-list" and "out-file" pipeline from c#? ): 我已经尝试了以下方法(基于此, 如何通过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);

But I get: 但是我得到:

Unhandled Exception: System.Management.Automation.RemoteException: Script block literals are not allowed in restricted language mode or a Data section. 未处理的异常:System.Management.Automation.RemoteException:在受限语言模式或“数据”部分中不允许使用脚本块文字。

Also I tried, 我也尝试过

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();

But not luck: 但不是运气:

Unhandled Exception: System.Management.Automation.RemoteException: The term 'Sort-Object' is not recognized as the name of a cmdlet 未处理的异常:System.Management.Automation.RemoteException:无法将术语“排序对象”识别为cmdlet的名称

I wonder if I should use multiple pipelines (one pipeline per cmdlet), but I am not sure. 我想知道是否应该使用多个管道(每个cmdlet一个管道),但是我不确定。

It sounds like the problem is the runspace. 听起来问题出在运行空间。 If that's an Exchange server, and you're running that in the remote management session provided by Exchange, the only thing you can do in that session is run the Exchange cmdlets. 如果这是一台Exchange服务器,并且您正在Exchange提供的远程管理会话中运行该服务器,则在该会话中唯一可以做的就是运行Exchange cmdlet。 The Select-Object and Sort-Object cmdlets and other PowerShell language elements just aren't there to use. 仅不使用Select-ObjectSort-Object cmdlet以及其他PowerShell语言元素。

Considering that Sort-Object is a command which is not recognized by the schema named ' http://schemas.microsoft.com/powershell/Microsoft.Exchange " then I proceed to develop a function using Snap-Ins and it's working fine. 考虑到“排序对象”是无法被名为“ http://schemas.microsoft.com/powershell/Microsoft.Exchange ”的架构识别的命令,因此我继续使用管理单元来开发一个功能,并且该方法工作正常。

Notice I'm taking the first database because the default sort mode is ascending. 注意,我正在使用第一个数据库,因为默认排序模式是递增的。 Also I'd like to comment that if you compile on Framework 4.0 you're going to get a "Value cannot be null error message" so you have to change to 3.5. 我还要评论一下,如果在Framework 4.0上编译,您将收到“ Value不能为空错误消息”,因此必须更改为3.5。

Keep in mind that it is being used by a WCF Service so no problem with Snap-Ins. 请记住,WCF服务正在使用它,因此管理单元没有问题。 If you like to use it on any other application, like a console-based application then you should install EMS 2010 on that computer. 如果要在其他任何应用程序(例如基于控制台的应用程序)上使用它,则应在该计算机上安装EMS 2010。

This function basically execute the following PowerShell command, Get-MailboxDatabase -Status | 此函数基本上执行以下PowerShell命令, Get-MailboxDatabase -Status | Sort-Object DatabaseSize 排序对象数据库大小

    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