title: How I can remote to my ActiveDirectory Server with C# and Powershell Class
body: <p>I must built a Webapplication that can add a new User in the ActiveDirectory about a powerhell command and I don't know how I can do this. </p>
  
  <p>If I do this for the exchangeserver it works. </p>
  
  <p>here my code for add contact in exchange Server:</p>
  
  <pre><code>string URL = "http://server141.test-company.com/PowerShell/";
  string Schema ="http://schemas.microsoft.com/powershell/Microsoft.Exchange";
  
  public string CreateRemoteConnectionToExchange(string UserName, string Password, string Mailbox)
          {
                  SecureString SecurePassword = new SecureString();
  
                  string str_password = Password;
                  string str_username = UserName;
  
                  foreach (char x in str_password)
                  {
                      SecurePassword.AppendChar(x);
                  }
  
                  PSCredential cred = new PSCredential(str_username, SecurePassword);
  
                  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(URL), Schema, cred);
  
                  connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
  
                  Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
  
                  PowerShell powershell = PowerShell.Create();
                  PSCommand command = new PSCommand();
  
                  command.AddCommand("New-MailContact");
                  command.AddParameter("ExternalEmailAddress", "SMTP:" + Mailbox + MailExtension);
                  command.AddParameter("Name", Mailbox);
                  command.AddParameter("Alias", Mailbox);
                  command.AddParameter("FirstName", Mailbox);
                  command.AddParameter("Initials", "");
                  command.AddParameter("LastName", "");
                  command.AddParameter("OrganizationalUnit", OrganizationalUnit);
                  command.AddParameter("DomainController", configDC);
  
                  powershell.Commands = command;
  
                  try
                  {
                      runspace.Open();
  
                      powershell.Runspace = runspace;
  
                      powershell.Invoke();
  
  
                      return "Der Kontakt wurde Erfolgreich erstellt";
                  }
                  catch (Exception ex)
                  {
                      ...
  
                  }
                  finally
                  {
                      runspace.Dispose();
                      runspace = null;
                      powershell.Dispose();
                      powershell = null;
                  }
  
  
          }
  </code></pre>
  
  <p>I donÄt know how I can make a Remote to the ActiveDirectory :/
  I need the URL and the Schema String. The ActiveDirectory is on <a href="http://server137.linde-wiemann.com/" rel="nofollow">http://server137.linde-wiemann.com/</a> ? = OU=NPS,OU=services,DC=test-company,DC=com or LDAP://OU=NPS,OU=services,DC=test-company,DC=com </p>
  
  <p>Can everyone help me</p>