簡體   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