title: C# Remote Powershell SharePoint 2010
body: <p>I'm trying to run powershell commands on a remote sharepoint server from a c# console app.  This is the code I have so far.  It runs without errors but does nothing.  What am I doing wrong?</p>
  
  <p>Thanks</p>
  
  <p>server name, username password, and url were taken out</p>
  
  <pre><code>    public static string RunScript()
      {
  
         Runspace remoteRunspace = null;
         openRunspace("http://server/wsman",
              "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
              @"domain\user",
              "password",
              ref remoteRunspace);
         try
         {
             StringBuilder stringBuilder = new StringBuilder();
             using (PowerShell powershell = PowerShell.Create())
             {
  
                 powershell.Runspace = remoteRunspace;
                 powershell.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
                 powershell.AddScript("enable-SPFeature -identity \"2dfc204b-e9da-4c6c-8b4f-c2f7c593ad4e\" -url sharepointsite -Confirm:$False");
                 powershell.Invoke();
                 Collection&lt;PSObject&gt; results = powershell.Invoke();
                 remoteRunspace.Close();
                 foreach (PSObject obj in results)
                 {
                     stringBuilder.AppendLine(obj.ToString());
                 }
             }
  
             return stringBuilder.ToString();
         }
         catch (Exception e)
         {
  
             return "";
  
         }
  
  
       }
  
      public static void  openRunspace(string uri, string schema, string username, string livePass, ref Runspace remoteRunspace)
      {
          System.Security.SecureString password = new System.Security.SecureString();
          foreach (char c in livePass.ToCharArray())
          {
              password.AppendChar(c);
          }
          PSCredential psc = new PSCredential(username, password);
          WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);
          rri.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
          rri.ProxyAuthentication = AuthenticationMechanism.Negotiate;
          remoteRunspace = RunspaceFactory.CreateRunspace(rri);
          remoteRunspace.Open();
      }
  </code></pre>