none
Getting Errors from Pipeline in C# RRS feed

  • Question

  • Hello,

    I am writing a some code to help issue commands to the our Exchange 2010 server via C#. I can issue the commands without a problem. But when there is a problem, I can't seem to get the error. Any help is much appreciated!

     

    My Code:

    XmlDocument originalXml = new XmlDocument();
                Runspace runspace = null;
    
                try
                {
    
                    XmlNode ResponseNode = originalXml.CreateNode(XmlNodeType.Element, "Response", null);
    
    
                    runspace = GlobalVars.GetRunspace();
                    runspace.Open();
    
    
                    Pipeline pipeLine = runspace.CreatePipeline();
    
    
                    List<string> EmailAddressList = new List<string>();
                    foreach (string eAddress in EmailAddresses.Split(','))
                    {
                        EmailAddressList.Add(eAddress);
                    }
    
    
    
                    Command GetMailbox = new Command("get-mailbox");
                    GetMailbox.Parameters.Add(new CommandParameter("organization", OrgName));
                    GetMailbox.Parameters.Add(new CommandParameter("identity", Alias));
    
                    Command SetMailbox = new Command("set-mailbox");
                    SetMailbox.Parameters.Add(new CommandParameter("EmailAddressPolicyEnabled", false));
                    SetMailbox.Parameters.Add(new CommandParameter("EmailAddresses", EmailAddressList.ToArray()));
                    //SetMailbox.Parameters.Add(new CommandParameter("PrimarySmtpAddress", PrimarySMTPAddress));
    
                    pipeLine.Commands.Add(GetMailbox);
                    pipeLine.Commands.Add(SetMailbox);
    
                    System.Collections.ObjectModel.Collection<PSObject> objs = pipeLine.Invoke();
    
    
                    XmlNode SetMailboxNode = originalXml.CreateNode(XmlNodeType.Element, "SetEmailAddresses", null);
    
    
                    if (pipeLine.Error.Count > 0)
                    {
                        XmlNode ErrorNode = originalXml.CreateNode(XmlNodeType.Element, "Error", null);
                        ErrorNode.InnerText = "Error!"; //I would like to display more error information HERE
    
                        SetMailboxNode.AppendChild(ErrorNode);
                    }
                    else
                    {
                        XmlNode SuccessNode = originalXml.CreateNode(XmlNodeType.Element, "Success", null);
                        SuccessNode.InnerText = "Email Addresses Updated";
                        SetMailboxNode.AppendChild(SuccessNode);
                    }
    
                    ResponseNode.AppendChild(SetMailboxNode);
    
    
                    originalXml.AppendChild(ResponseNode);
    
                }
                catch (RemoteException re)
                {
                    XmlNode ErrorNode = originalXml.CreateNode(XmlNodeType.Element, "Error", null);
                    ErrorNode.InnerText = re.Message;
                    originalXml.AppendChild(ErrorNode);
                }
                catch (Exception ex)
                {
                    XmlNode ErrorNode = originalXml.CreateNode(XmlNodeType.Element, "Error", null);
                    ErrorNode.InnerText = ex.Message;
                    originalXml.AppendChild(ErrorNode);
    
                }
    
                if (runspace != null)
                {
                    runspace.Close();
                }
                return originalXml;
    
    
            }
    

    Also if this isn't the best way to issue commands to the exchange server please let me know.

    Tuesday, September 6, 2011 7:26 PM

All replies

  • Update,

     

    When changing the follow:

    if (pipeLine.Error.Count > 0)
                    {
                        XmlNode ErrorNode = originalXml.CreateNode(XmlNodeType.Element, "Error", null);
                        ErrorNode.InnerText = "Error!";
    
                        foreach (object item in pipeLine.Error.ReadToEnd())
                        {
    
                            ErrorNode.InnerText += " " + item.ToString();
                        }
    
    
                        SetMailboxNode.AppendChild(ErrorNode);
                    }
    

    I get "Specified method is not supported." in the catch statement.

    Tuesday, September 6, 2011 7:29 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<PSObject> results = pipeline.Invoke();
    
    Collection<ErrorRecord> errors = pipeline.Streams.Error.ReadAll();
    
    if (errors != null || errors.Count > 0)
    {
       foreach (ErrorRecord er in errors)
       {
          Console.WriteLine(er.Exception.ToString());
       }
    }
    

    This wont be availble in your catch block at that level you have a more Syntax or Application fault type error happening
    cheers
    Glen 
    Wednesday, September 7, 2011 3:29 AM
  • Hi,

    I faced this issue of Pipeline.Error.ReadToEnd() and saw that there is no property with the name "Stream" for pipeline object. I checked this both on .net framework 2.0 and 4.0. I was having this error against Exchange 2013 Remote PowerShell and found the solution here:

    http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopmentlegacy/thread/b2bece71-72d7-4305-ad81-02139959e643

    which is:

    if (pipeline.Error.Count > 0)
    {
           var error = pipeline.Error.Read() as Collection<ErrorRecord>;
           if (error != null)
           {
               foreach (ErrorRecord er in error)
               {
                    Console.WriteLine("[PowerShell]: Error in cmdlet: " + er.Exception.Message);
               }
           }
    }

    Thanks and Regards,

    Laeeq Qazi


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com

    Monday, December 24, 2012 4:37 PM
  • Hi,

    I just came to know that Stream property is available with PowerShell class and when we run PowerShell.Invoke() instead of Pipeline.Invoke() like this:

    using (Runspace rs = RunspaceFactory.CreateRunspace(wsManInfo))
    {
       rs.Open();
      PowerShell psh = PowerShell.Create();
      psh.Runspace = rs;
      PSCommand getMailbox = new PSCommand();            
    
      getMailbox.AddCommand("Get-Mailbox");
      getMailbox.AddParameter("Resultsize", "Unlimited");
    
      psh.Commands = getMailbox;
      Collection<PSObject> results = psh.Invoke();
      if (psh.Streams.Error.Count > 0)
      {
          foreach (ErrorRecord err in psh.Streams.Error)
          {                    
            // handle the error some how                
          }
          rs.Close(); 
      }        
    }

    Thanks and Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com


    • Edited by Laeeq Qazi Monday, December 24, 2012 5:05 PM
    Monday, December 24, 2012 5:04 PM