Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#和Powershell类远程访问ActiveDirectory服务器_C#_Asp.net_Powershell_Active Directory_Remote Server - Fatal编程技术网

如何使用C#和Powershell类远程访问ActiveDirectory服务器

如何使用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

我必须构建一个Webapplication,它可以在ActiveDirectory中添加一个关于powerhell命令的新用户,我不知道如何才能做到这一点

如果我为exchangeserver执行此操作,它将正常工作

以下是我在exchange Server中添加联系人的代码:

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几乎所有的功能