質問

I'm trying to get the lower store from a 2010 Exchange server, and the function will run in a WCF container.

The problem I'm facing is that I'm unable to run multiple PowerShell commands in the pipeline.

I've tried the following (based on this, 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);

But I get:

Unhandled Exception: System.Management.Automation.RemoteException: Script block literals are not allowed in restricted language mode or a Data section.

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

I wonder if I should use multiple pipelines (one pipeline per cmdlet), but I am not sure.

役に立ちましたか?

解決

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. The Select-Object and Sort-Object cmdlets and other PowerShell language elements just aren't there to use.

他のヒント

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.

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.

Keep in mind that it is being used by a WCF Service so no problem with Snap-Ins. If you like to use it on any other application, like a console-based application then you should install EMS 2010 on that computer.

This function basically execute the following PowerShell command, 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帰属
所属していません StackOverflow
scroll top