title: C# Application Run PowerShell Multiple Remote Commands
body: <p>I have a simple console app trying to create a distribution group in Exchange via PowerShell and add a few members to it. </p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>class Program
  {
      static void Main(string[] args)
      {
          string userName = "foo";
          string password = "pwd";
  
          // Encrypt password using SecureString class
          SecureString securePassword = new SecureString();
  
          foreach (char c in password)
          {
              securePassword.AppendChar(c);
          }
  
          PSCredential credential = new PSCredential(userName, securePassword);
  
          // Connection information object required to connect to the service
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
              new Uri("https://ps.outlook.com/powershell"), 
              "http://schemas.microsoft.com/powershell/Microsoft.Exchange", 
              credential);
  
          connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
          connectionInfo.MaximumConnectionRedirectionCount = 2;
  
  
  
          // Create runspace on remote Exchange server
          using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
          {
              runspace.Open();
  
              using(PowerShell ps = PowerShell.Create())
              {
                  ps.Runspace = runspace;
  
                  Command newDG = new Command("New-DistributionGroup");
                  newDG.Parameters.Add(new CommandParameter("Name", "Test"));
                  ps.Commands.AddCommand(newDG);
  
                  Command addDGMember1 = new Command("Add-DistributionGroupMember");
                  addDGMember1.Parameters.Add(new CommandParameter("Identity", "Test"));
                  addDGMember1.Parameters.Add(new CommandParameter("Member", "testuser1@somecompany.com"));
                  ps.Commands.AddCommand(addDGMember1);
  
                  Command addDGMember2 = new Command("Add-DistributionGroupMember");
                  addDGMember2.Parameters.Add(new CommandParameter("Identity", "Test"));
                  addDGMember2.Parameters.Add(new CommandParameter("Member", "testuser2@somecompany.com"));
                  ps.Commands.AddCommand(addDGMember2);
  
  
                  try
                  {
                      // Invoke command and store the results in a PSObject
                      Collection&lt;PSObject&gt; results = ps.Invoke();
  
                      if (ps.Streams.Error.Count &gt; 0)
                      {
                          foreach (ErrorRecord error in ps.Streams.Error)
                          {
                              Console.WriteLine(error.ToString());
                          }
                      }
  
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                  }
                  finally
                  {
                      Console.WriteLine("Operation completed.");
                  }
  
              }
          }
  
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>When I run my app it throws this error: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.</p>
  
  <p>But the distribution group actually gets created. </p>
  
  <p>Also, I notice that when I comment out the command to create a new distribution group and run the commands to add the distribution group members, only the first member is added. I'm really confused as to how I should proceed on this and I have the following questions:</p>
  
  <ol>
  <li><p>How can I get my code to run all the commands successfully?</p></li>
  <li><p>What would be the best way to do multiple remote PowerShell commands? Do I run each command separately, check the return object if it was successful, and then proceed to the next command. Any performance issues to be aware of?</p></li>
  <li><p>Does the runspace run only one command at a time?</p></li>
  </ol>
  
  <p>When I try the following code, I run into this error:
  <em>The syntax is not supported by this runspace. This might be because it is in no-
  language mode.</em></p>
  
  <pre class="lang-cs prettyprint-override"><code>using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
          {
              runspace.Open();
  
              using(PowerShell ps = PowerShell.Create())
              {
                  ps.Runspace = runspace;
  
                  Pipeline pipe = runspace.CreatePipeline();
                  pipe.Commands.AddScript("New-DistributionGroup -Name Test2");
  
                  try
                  {
                      Collection&lt;PSObject&gt; results = pipe.Invoke();
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                  }
              }
        }
  </code></pre>