none
Return Remote Powershell Errors with C# RRS feed

  • Question

  • Hi everyone

    I'm trying to figure out how to return any errors that are encountered when remotely executing PowerShell via C# to Enable Exchange 2010 mailboxes. Here is the code I'm using below:

    runspace.Open();
    powershell.Runspace = runspace;
    
    command.AddCommand("Enable-Mailbox");
    command.AddParameter("Identity", up.SamAccountName);
    
    powershell.Commands = command;
    Collection<PSObject> results = powershell.Invoke();
    


    I know that the Collection<PSObject> should get information back upon the Invoke(), but how do I check if all was ok or if there were errors in Enabling the mailbox? I can see the errors on the server event logs in the 'MSExchange Management' logs

    Cmdlet failed. Cmdlet Enable-Mailbox, parameters {Identity=(samAccountName}.Event ID 6

    Thanks in advance for anyone that can help!

    • Moved by Larcolais Gong Wednesday, July 13, 2011 6:28 AM (From:Visual C# General)
    • Moved by Martin_Xie Thursday, July 14, 2011 4:03 AM Move it to Exchange Server Development Forum for better support. (From:Off-Topic Posts (Do Not Post Here))
    Tuesday, July 12, 2011 8:33 PM

Answers

  • Glen,

    Yes! That is exactly what I was looking for thank you very much! Here is what I did (using log4net for logging):

    Collection<PSObject> results = powershell.Invoke();
    
    Collection<ErrorRecord> errors = powershell.Streams.Error.ReadAll();
    
    if (errors != null || errors.Count > 0)
    {
       foreach (ErrorRecord er in errors)
       {
          log.Error(er.Exception.ToString());
       }
    }
    

    Thanks again and I hope this post proves useful to others

    • Marked as answer by dlackey Tuesday, July 19, 2011 6:29 PM
    Tuesday, July 19, 2011 6:27 PM
  • Any errors that occur within the session your executing should be available in the streams property http://msdn.microsoft.com/en-us/library/dd128213%28v=VS.85%29.aspx so in your example you should just be able to do

     

    Collection<ErrorRecord> errors = powershell.Streams.Error.ReadAll();

    Cheers
    Glen

    • Marked as answer by dlackey Tuesday, July 19, 2011 6:27 PM
    Friday, July 15, 2011 2:57 AM

All replies

  • You probably want to look at using Exchange SDK itself then powershell.

    http://msdn.microsoft.com/en-us/exchange/default.aspx

    chanmm


    chanmm
    Wednesday, July 13, 2011 2:32 AM
  • Hi there

    Thanks for the response, but in order to use the Exchange SDK you have to use the Management Shell SDK to enable mailboxes....which is an extension of Windows PowerShell.

    Also, to Larcolais Gong, not sure why you moved my thread? This is a C# syntax question, which is why it was posted in C# General.

    Wednesday, July 13, 2011 6:19 PM
  • Hello dlackey,

    From your provided

    "Cmdlet failed. Cmdlet Enable-Mailbox, parameters {Identity=(samAccountName}.Event ID 6"

    It looks that this issue may be related to Exchange server. http://social.technet.microsoft.com/Forums/en-US/exchange2010/thread/3cac5ec0-4bfc-4611-9d1f-7dc583feefa8

    I helped you move your thread into Exchange forum. You will get more helpful suggestions and troubleshooting there.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 2:50 AM
  • Larcolais,

    You are incorrect. My original post is asking a question about how to return the errors from the Exchange server to my C# code via the Invoke() method (PowerShell) on another system. This is not a question about the error itself on Exchange server, but a question of HOW to return that error to C# code so that I can catch it.

    Please move this back to C# development area, so that it may be properly answered.

    Thursday, July 14, 2011 1:51 PM
  • Any errors that occur within the session your executing should be available in the streams property http://msdn.microsoft.com/en-us/library/dd128213%28v=VS.85%29.aspx so in your example you should just be able to do

     

    Collection<ErrorRecord> errors = powershell.Streams.Error.ReadAll();

    Cheers
    Glen

    • Marked as answer by dlackey Tuesday, July 19, 2011 6:27 PM
    Friday, July 15, 2011 2:57 AM
  • Glen,

    Yes! That is exactly what I was looking for thank you very much! Here is what I did (using log4net for logging):

    Collection<PSObject> results = powershell.Invoke();
    
    Collection<ErrorRecord> errors = powershell.Streams.Error.ReadAll();
    
    if (errors != null || errors.Count > 0)
    {
       foreach (ErrorRecord er in errors)
       {
          log.Error(er.Exception.ToString());
       }
    }
    

    Thanks again and I hope this post proves useful to others

    • Marked as answer by dlackey Tuesday, July 19, 2011 6:29 PM
    Tuesday, July 19, 2011 6:27 PM