none
Using C# to manage Exchange using powershell automation Error RRS feed

  • Question

  • Hello,
    I have the following code which "should" just return the results of a get-mailbox command. However I am getting the following error when I invoke the pipeline. Any ideas on what might be causing this?

    Thanks

    Bill

    Value cannot be null.
    Parameter name: parameters

    at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
       at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecute(Array input, Hashtable errorResults)
       at System.Management.Automation.Internal.PipelineProcessor.Execute(Array input)
       at System.Management.Automation.Internal.PipelineProcessor.Execute()
       at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
       at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()






               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-Mailbox");
                    getmailbox.Parameters.Add(new CommandParameter("Identity", mailboxIdentity));
                    Pipeline commandPipeLine = runspace.CreatePipeline();
                    commandPipeLine.Commands.Add(getmailbox);
                    Collection<PSObject> getmailboxResults = commandPipeLine.Invoke();
     
                    if (getmailboxResults.Count > 0)
                    {
     
                        foreach (PSObject getMailboxResult in getmailboxResults)
                        {
     
                        }
                    }
                }
                catch (Exception exp)
                {
                    string expectionText = exp.Message;
                    string innerExceptionText = exp.InnerException.Message;
                    string stackTrace = exp.StackTrace;
                }

    • Edited by williamtholmes Friday, February 3, 2012 12:47 AM
    • Moved by Leo Liu - MSFT Friday, February 3, 2012 5:28 AM Moved for better support. (From:Visual C# General)
    Friday, February 3, 2012 12:46 AM

Answers

  • The key here is to use remote powershell. To setup a remote powershell runspace for exchange 2010 you can use the following c# code:


                PSCredential psCredential = (PSCredential)null
                WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchangeServerName/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential);             connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.             connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.             Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

    One thing to note here is the use of the (PSCrendital)null for the crendential. This causes the runspace to use the current credential for making the connection to the remote server using Windows Authentication. Also note that you need to use the correct schema if you expect the remote server to process Exchagne commandlets.
    Bill
    Monday, February 6, 2012 8:17 PM

All replies

  • This may, or may not, help you.  I don't have an Exchange 2010 currently up to test with, so I tested with Exchange 2007.  The only thing that I changed was:

    PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
    

    to:

    PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
    

    and everything tested fine.  For testing, I was on the Exchange Box itself and logged in with Exchange Administrator rights.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
    Friday, February 3, 2012 2:40 AM
  • Hello,

    In order to manage exchange 2010 you need use the Exchange 2010 Extensions. The 2007 extensions will not manage Exchagne 2010 objects.

    Bill

    Friday, February 3, 2012 3:17 AM
  • Here is another person who was having similar issues and decided to use the remote powershell to solve:

    http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/48da1346-f47e-4ba9-9747-428fe07b4492/


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
    Friday, February 3, 2012 3:18 AM
  • Hello,

    In order to manage exchange 2010 you need use the Exchange 2010 Extensions. The 2007 extensions will not manage Exchagne 2010 objects.

    Bill


    I figured that, but I just thought it might be helpful to know that the code is sound.  I also came across articles that there was a known issue with 2010 and a patch to fix it, it was referring to web services, but who knows how it could be interconnected, especially since the other link that I posted, they basically started accessing it through http to solve.

    The exceptions that you were getting were specifically mentioned.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
    Friday, February 3, 2012 3:21 AM
  • William, thank you for the question and Thomas, thank you for answering.

     

    The purpose of this forum is to support the Open Specifications documentation. You can read about the Microsoft Open Specifications program at http://www.microsoft.com/openspecifications/en/us/default.aspx.  The library of Open Specification documents is located at http://msdn.microsoft.com/en-us/library/dd208104(PROT.10).aspx.

     

    Since this does not appear to be a protocol question, we would polity recommend the Exchange Server Development forum at http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/threads for future threads on this topic or follow-up on this question.


    Bryan S. Burgin Senior Escalation Engineer Microsoft Protocol Open Specifications Team
    Friday, February 3, 2012 5:10 PM
    Moderator
  • I didn't put this thread here It was moved by:

    Moved byLeo Liu - MSFT<abbr class="affil">Microsoft Contingent Staff</abbr>13 hours 29 minutes agoMoved for better support. (From:Visual C# General)

    Friday, February 3, 2012 6:59 PM
  • William, thank you for the question and Thomas, thank you for answering.

     

    The purpose of this forum is to support the Open Specifications documentation. You can read about the Microsoft Open Specifications program at http://www.microsoft.com/openspecifications/en/us/default.aspx.  The library of Open Specification documents is located at http://msdn.microsoft.com/en-us/library/dd208104(PROT.10).aspx.

     

    Since this does not appear to be a protocol question, we would polity recommend the Exchange Server Development forum at http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/threads for future threads on this topic or follow-up on this question.


    Bryan S. Burgin Senior Escalation Engineer Microsoft Protocol Open Specifications Team


    Hello,

    Would you please ask Leo Liu to Not move my posts in the future? Again I did not place this thread here to begin with. I have no idea why it was moved from the C# forum.

    Thanks

    Friday, February 3, 2012 7:46 PM
  • Thank you for the feedback.  I'll follow-up off line.

     


    Bryan S. Burgin Senior Escalation Engineer Microsoft Protocol Open Specifications Team
    Friday, February 3, 2012 9:30 PM
    Moderator
  • The key here is to use remote powershell. To setup a remote powershell runspace for exchange 2010 you can use the following c# code:


                PSCredential psCredential = (PSCredential)null
                WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchangeServerName/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential);             connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.             connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.             Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

    One thing to note here is the use of the (PSCrendital)null for the crendential. This causes the runspace to use the current credential for making the connection to the remote server using Windows Authentication. Also note that you need to use the correct schema if you expect the remote server to process Exchagne commandlets.
    Bill
    Monday, February 6, 2012 8:17 PM