繁体   English   中英

是否可以使用c#/ powershell将AD模块导入到现有的Exchange运行空间中?

[英]Is it possible to import the AD module into an existing Exchange runspace using c#/powershell?

我正在创建一个交换用户(新邮箱),然后在同一运行空间中创建该用户后,使用在Exchange运行空间中除非运行import-module'activedirecty'才会运行的命令在用户上设置一些AD参数。 是否可以像创建Powershell提示一样在创建运行空间后导入模块?

在我要运行的同一运行空间会话中:

new-mailbox
set-mailbox
set-user
set-aduser 

最后一个是什么要求我导入AD模块,我可以直接在Powershell中成功运行命令,但是似乎无法弄清楚如何在运行空间会话中添加模块? 我试过了

powershell.AddParameter("import-module -name 'activedirectory'; set-aduser xxxx")

powershell.AddParameter("import-module -name 'activedirectory'")
powershell.AddParameter("set-aduser xxxx")

powershell.AddScript("import-module -name 'activedirectory'; set-aduser xxxx")

这在下面工作

public void SetPasswordNeverExpiresProperty(bool PasswordNeverExpires, string alias)
    {           
        string dn = "CN=xxx,OU=xxx,OU=xxx=xxx=xxx=xxx,DC=xx,DC=xx,DC=xxx,DC=xxx"

        DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + dn);
        ArrayList props = new ArrayList();
        int NON_EXPIRE_FLAG = 0x10000;
        int EXPIRE_FLAG = 0x0200;
        int valBefore = (int) objRootDSE.Properties["userAccountControl"].Value;            
        objRootDSE.Properties["userAccountControl"].Value = EXPIRE_FLAG;
        objRootDSE.CommitChanges();
        string valAfter = objRootDSE.Properties["userAccountControl"].Value.ToString();`

而且我猜不到,任何帮助将不胜感激。

        PSCredential ExchangeCredential = new PSCredential(PSDomain + @"\" + PSUsername, PSpwd);
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("xxxxxx/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;


        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            PowerShell powershell = PowerShell.Create();

                if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
                {
                    // do nothing
                }
                else
                {
                    runspace.Open();
                    powershell.Runspace = runspace;
                }
            try
                {
                    psobjs = powershell.Invoke();
                }
                catch (Exception ex)
                {
                    result = "Failed: " + ex.Message;
                }

                powershell.Commands.Clear();

        }

在此处输入图片说明

我将在评论中总结我的评论,因为看来我出乎意料的帮助了:)

我还发现,使用像这样的远程PowerShell时不能使用Import-Module 这有点烦人,但生活就是这样。

几年前,我在我们的环境中为AD和Exchange 2010实现了自动帐户创建服务。我发现必须使用DirectoryEntry进行AD帐户操作,然后使用PowerShell仅进行Exchange DirectoryEntry

问题是要确保这两种情况都发生在同一个域控制器上,这样就不会遇到复制问题。

因此,您有两个选择:使用“ New-Mailbox一次性创建邮箱和AD帐户。 如您所指出的,结果的OriginatingServer属性具有域控制器。 但是那里也有一个DistinguishedName属性! (当您提到服务器属性时,我才发现了这一点),然后可以针对相同的域控制器创建DirectoryEntry对象,如下所示:

new DirectoryEntry($"LDAP://{domainController}/{distinguishedName}")

或者,我所做的(我想是因为当时我没有意识到我可以从New-Mailbox的结果中获取DC),首先使用DirectoryEntry创建AD对象,然后从中获取创建的域控制器。 .Options.GetCurrentServerName() ,然后将其在DomainController参数中传递给Enable-Mailbox

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM