简体   繁体   中英

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

I am creating an exchange user (new-mailbox) and then setting some AD parameters on them after the user is created in the same runspace with commands that will not run in the Exchange runspace unless import-module 'activedirecty' is ran. Is there a way to import the module after the runspace is created as I can do with the Powershell prompt?

inside the same runspace session I want to run:

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

The last one is what requires me to import the AD module I can successfully run the command inside of Powershell directly, but can't seem to figure out how to add the module mid runspace session? I'd tried

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

and

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

and

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

This works below

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();`

And I'm out of guesses, any help would be appreciated.

        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();

        }

在此处输入图片说明

I'll sum up my comments in an answer, since it seems I was unexpectedly helpful :)

I also had found that you can't use Import-Module when using remote PowerShell like that. It's kind of annoying, but such is life.

Years ago, I implemented an automatic account creation service in our environment for AD and Exchange 2010. I found I had to do the AD account manipulation with DirectoryEntry and then only the Exchange stuff with PowerShell.

The problem is making sure that both things happen on the same domain controller so you don't run into replication problems.

So you have two options: Use New-Mailbox to create the mailbox and AD account in one shot. As you pointed out, the OriginatingServer property of the result has the domain controller. But there is also a DistinguishedName property there too! (I just found this when you mentioned the server property) Then you can create a DirectoryEntry object against the same domain controller like this:

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

Or, what I did (I think because I didn't realize at the time that I could get the DC from the result of New-Mailbox ), is create the AD object first with DirectoryEntry , pull the domain controller it got created on from .Options.GetCurrentServerName() , then pass that in the DomainController parameter to Enable-Mailbox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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