Hi,

The following code is from a listener service I built to do the enable=mailbox command on Exchange 2010. If I have a syntax error in the PowerShell command sent to Exchange, I get an error reported back, which is just what I would expect. However, my test data is deliberately bad (I'm using bogus database names). Looking in the event logs on the Exchange server, the appropriate errors are logged. But I am not getting an error back. While I don't ever expect this command to actually fail, I still want to be able to report back to the user in real-time if it did. Any ideas what I'm missing?

Code:
      TcpClient client = listener.AcceptTcpClient();
      NetworkStream ns = client.GetStream();

      StreamWriter writer = new StreamWriter(ns);
      StreamReader reader = new StreamReader(ns);

      // Set the connection Info
      WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
          new Uri("https://mail.linfield.edu/powershell"),
          "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
          credential);

      connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
      Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

      PowerShell powershell = PowerShell.Create();
      PSCommand command = new PSCommand();
      command.AddCommand("enable-Mailbox");
      command.AddParameter("DomainController", "fenrir.catnet.wfo.linfield.edu");

      try
      {
          String inString = reader.ReadLine().Trim();
          eventLog.WriteEntry(inString);

          String[] parms = inString.Split(':');
          command.AddParameter("identity", parms[0]);
          command.AddParameter("database", parms[1]);
      }
      catch (Exception e)
      {
          eventLog.WriteEntry(e.Message);
      }

      powershell.Commands = command;
      runspace.Open();

      powershell.Runspace = runspace;

      Collection<PSObject> results = new Collection<PSObject>();
      try
      {                  
          results = powershell.Invoke();
       }
       catch (Exception ex)
       {
          results.Add(new PSObject((object)ex.Message));
        }
                
       // close the runspace
       runspace.Close();

       // convert the script result into a single string
      StringBuilder stringBuilder = new StringBuilder();

       foreach (PSObject obj in results)
       {
          stringBuilder.AppendLine(obj.ToString());
       }

       writer.WriteLine(stringBuilder.ToString());
       writer.Flush();

        ns.Close();
        client.Close();
Thanks,

Rob Tanner
Linfield College