0

我必须构建一个 Web 应用程序,它可以在 ActiveDirectory 中添加一个关于 powerhell 命令的新用户,但我不知道我该怎么做。

如果我为交换服务器这样做,它就可以工作。

这是我在交换服务器中添加联系人的代码:

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;
                }


        }

我不知道如何对 ActiveDirectory 进行远程操作:/ 我需要 URL 和架构字符串。ActiveDirectory 在http://server137.linde-wiemann.com/上?= OU=NPS,OU=services,DC=test-company,DC=com 或 LDAP://OU=NPS,OU=services,DC=test-company,DC=com

大家可以帮帮我吗

4

1 回答 1

0

我发现这段关于活动目录的代码是这个链接,它显示了几乎所有关于活动目录的功能。

http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C

于 2013-02-28T09:13:52.320 回答