title: Exchange Managed Shell from C#
body: <p>I'm trying to run next script remotely from client computer:</p>
  
  <blockquote>
    <p>Get-Mailbox | foreach { Get-InboxRule -Mailbox $_.Name |
    Remove-InboxRule }</p>
  </blockquote>
  
  <p>There is my code:</p>
  
  <pre><code>private const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
  
      static public void RunPowerShellScript(string scriptfile, List&lt;CommandParameter&gt; cmdList, string runasUsername, string runasPassword, string serverIP)
      {
          // Prepare the credentials that will be used when connecting
          // to the server. More info on the user to use on the notes
          // below this code snippet.
  
          System.Uri serverUri = new Uri(String.Format("http://{0}/POWERSHELL?serializationLevel=Full", serverIP));
          System.Security.SecureString securePassword = new System.Security.SecureString();
  
          foreach (char c in runasPassword.ToCharArray())
          {
              securePassword.AppendChar(c);
          }
  
          System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(runasUsername, securePassword);
  
          RunspaceConfiguration rc = RunspaceConfiguration.Create();
          WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
          wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
          RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
  
          Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
          runspace.Open();
  
          RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
  
          Pipeline pipeline = runspace.CreatePipeline();
  
          //Here's how you add a new script with arguments
          Command myCommand = new Command(scriptfile, true);
  
          if(cmdList != null)
          {
              foreach (var cmd in cmdList)
                  myCommand.Parameters.Add(cmd);
          }
  
          pipeline.Commands.Add(myCommand);
  
          // Execute PowerShell script
          var results = pipeline.Invoke();
      }
  </code></pre>
  
  <p>As a result, the last line throws exception:</p>
  
  <blockquote>
    <p>The term 'Get-Mailbox' is not recognized as the name of a cmdlet,
    function, script file, or operable program. Check the spelling of the
    name, or if a path was included, verify that the path is correct and
    try again.</p>
  </blockquote>
  
  <p>I know, that i'm using PowerShell there, not ExchangeManagedShell. I`ve tried to add snapin like this:</p>
  
  <pre><code>RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
  PSSnapInException snapInException = null;
  PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
  Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
  myRunSpace.Open(rsConfig);
  </code></pre>
  
  <p>but there is no ExchangeManagedShell on client computer.</p>
  
  <p>P.S. I'm trying to connect to Exchange2013.</p>
  
  <p>What im doing wrong?</p>
  
  <p><strong>[EDITION #1]</strong></p>
  
  <p>Oh, I've found out, what i'm doing wrong:</p>
  
  <pre><code>WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
  wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
  RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
  
  Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
  runspace.Open();
  </code></pre>
  
  <p>I even don't connect to remote computer. Creating <strong>wsManInfo</strong>, but don't use it in <strong>RunspaceFactory.CreateRunspace();</strong>.</p>
  
  <p>But i still don't know how to solve the problem... </p>