1

I have a question to C# and Powershell in a ASP.NET Application.

I want to create a User in our Active Directory and I want/must use a powershell for this. I had built a Webapllication that could add a mailcontact in the Exchange Server one year ago. For this I used the System.Management.Automation Namespace with the Powershell Classes. But I don't know how I can do it for the Active Directory.

the ps command that I must use:

New-ADUser %prefix-%name 
-SamAccountName "%name" 
-UserPrincipalName "%[email protected]" 
-GivenName "%text1" 
-SurName "%text2" 
-displayname "%name" 
-enabled $true 
-Path '%OU' 
-AllowReversiblePasswordEncryption $true 
-PasswordNeverExpires $true 
-AccountPassword (ConvertTo-Securestring "%name" -asplaintext -Force) 

and here my cs code:

public void CreateRemoteConnectionToActiveDirectory(string Username, string password//,...comming)
        {
            SecureString securePassword = new SecureString();

            str_password = password;
            str_username = Username;

            foreach (char x in str_password)
            {
                securePassword.AppendChar(x);
            }

            PSCredential cred = new PSCredential(str_username, securePassword); 

          //  connection?
        }

My Old Code for the exchange Server:

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(ExchangeServer), 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;
                }


        }

How I can do this. A Example,tutorial or a tipp would me help.

1 Answer 1

1

Looks like your connection just needs to connect to either your AD server or another server that can actually execute the command.

Sign up to request clarification or add additional context in comments.

2 Comments

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(ActiveDirectory), Schema, cred); For Example: ActiveDirectory = LDAP://OU=NPS,OU=services,DC=test-company,DC=com ? or how I built this connectionstring?
Tarasov, did you ever find out what the new URI should be for active directory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.