如何使用C#和Powershell类远程访问ActiveDirectory服务器
我必须构建一个Webapplication,它可以在ActiveDirectory中添加一个关于powerhell命令的新用户,我不知道如何才能做到这一点 如果我为exchangeserver执行此操作,它将正常工作 以下是我在exchange Server中添加联系人的代码:如何使用C#和Powershell类远程访问ActiveDirectory服务器,c#,asp.net,powershell,active-directory,remote-server,C#,Asp.net,Powershell,Active Directory,Remote Server,我必须构建一个Webapplication,它可以在ActiveDirectory中添加一个关于powerhell命令的新用户,我不知道如何才能做到这一点 如果我为exchangeserver执行此操作,它将正常工作 以下是我在exchange Server中添加联系人的代码: string URL = "http://server141.test-company.com/PowerShell/"; string Schema ="http://schemas.microsoft.com/pow
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和模式字符串。Active Directory已打开?=OU=NPS,OU=services,DC=test company,DC=com或LDAP://OU=NPS,OU=services,DC=test company,DC=com
每个人都能帮我吗?我找到了这个关于active directory的代码是这个链接,它显示了active directory几乎所有的功能