1

I need to import the ActiveDirectory module for one of my scripts. When I do this from the command line, running

Import-Module -Name ActiveDirectory
./script.ps1

Everything works just dandy.

But putting the import statement at the top of the script and running the script only, without importing from the command line results in the module not being imported at all, such that this class inside the module is not recognized:
Unable to find type [Microsoft.ActiveDirectory.Management.ADUser].

This problem doesn't come up when running the script from the VS code debugger. Why could this be happening?

2
  • that usually seems to be a privs problem. are you using the same account both times? is the account in use elevated both times? ///// have you tried enabling a transcript to see what errors your code generates?
    – Lee_Dailey
    Jul 13, 2020 at 15:46
  • Try the script in the PS ISE. If it works there, your issue is in VS Code.
    – Jeter-work
    Jul 13, 2020 at 19:44

1 Answer 1

-1

I had to go digging in C:\Windows\WinSxS to grab the needed DLLs.

DLLs:

Microsoft.ActiveDirectory.Management.dll

Microsoft.ActiveDirectory.Management.resources.dll

Folders:

amd64_microsoft.activedirectory.management_31bf3856ad364e35_10.0.14393.4880_en-us_74aaa3f6f858fbf7 amd64_microsoft.activedirectory.management_31bf3856ad364e35_10.0.14393.4880_none_8e0f8779d0e8e934

Instructions:

Once you have the dlls, stage them in C:\windows\system32\windowspowershell\v1.0\modules\activedirectory

Run something like

cd C:\windows\system32\windowspowershell\v1.0\modules\activedirectory

import-module .\Microsoft.ActiveDirectory.Management.dll

import-module .\Microsoft.ActiveDirectory.Management.resources.dll

#reimport ActiveDirectory; even if you've already done it

import-module ActiveDirectory

Run your script. Should work

Note:

In my testing, staging the dlls in ActiveDirectory and just importing the ActiveDirectory module didn't work for me. I had to import each; One after another. If you are doing this within a script, throw a Start-Sleep in between

4
  • This is unsupported, I think you shoud have nstalled the RSAT feature?
    – Turdie
    Oct 22, 2023 at 15:15
  • Turdie, are you saying that you tried following the directions in my comment and it failed for you? If so, please drop the exact error in comments and I'll take a look
    – LieSmith
    Oct 23, 2023 at 16:06
  • That doesn't matter, your just not following the best practices howto install a module, you don't want to do this in a production environment.
    – Turdie
    Oct 24, 2023 at 13:12
  • I see. Thank you for your contribution
    – LieSmith
    Oct 25, 2023 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .