我对ASP.NET应用程序中的C#和Powershell有疑问。
我想在Active Directory中创建一个用户,我希望/必须使用powershell。我在一年前构建了一个可以在Exchange Server中添加mailcontact的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;
}
}
我怎么做到这一点。我会帮助一个例子,教程或者tipp。
答案 0 :(得分:1)
看起来您的连接只需要连接到您的AD服务器或其他可以实际执行该命令的服务器。