【问题标题】:How I can create a User in AD with Powershell in a ASP.NET Webapplication如何在 ASP.NET Web 应用程序中使用 Powershell 在 AD 中创建用户
【发布时间】:2012-10-13 10:49:44
【问题描述】:

我对 ASP.NET 应用程序中的 C# 和 Powershell 有疑问。

我想在我们的 Active Directory 中创建一个用户,并且我想/必须为此使用 powershell。一年前,我构建了一个可以在 Exchange Server 中添加邮件联系人的 Webapllication。为此,我将 System.Management.Automation 命名空间与 Powershell 类一起使用。但我不知道如何为 Active Directory 做到这一点。

我必须使用的 ps 命令:

New-ADUser %prefix-%name 
-SamAccountName "%name" 
-UserPrincipalName "%name@test-company.com" 
-GivenName "%text1" 
-SurName "%text2" 
-displayname "%name" 
-enabled $true 
-Path '%OU' 
-AllowReversiblePasswordEncryption $true 
-PasswordNeverExpires $true 
-AccountPassword (ConvertTo-Securestring "%name" -asplaintext -Force) 

这里是我的cs代码:

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

我的交换服务器旧代码:

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


        }

我怎么能做到这一点。一个示例、教程或提示会对我有所帮助。

【问题讨论】:

    标签: c# asp.net powershell active-directory powershell-remoting


    【解决方案1】:

    看起来您的连接只需要连接到您的 AD 服务器或其他可以实际执行命令的服务器。

    【讨论】:

    • WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(ActiveDirectory), Schema, cred);例如:ActiveDirectory = LDAP://OU=NPS,OU=services,DC=test-company,DC=com ?或者我是如何构建这个连接字符串的?
    • Tarasov,你有没有发现活动目录的新 URI 应该是什么?
    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2014-08-04
    • 2010-09-27
    相关资源
    最近更新 更多