Home Powershell Activity

Error Using ActiveDirectory PowerShell Module

Nick_FlintNick_Flint Customer Advanced IT Monkey ✭✭✭

I have a PSA that calls ScriptA which acts as a traffic director. ScriptA inports the ActiveDirectory module. Based on the template ID ScriptA calls the associated ScriptB. In ScriptB I'm trying to use the Get-ADUser command:

 # $ADUser = Get-ADUser -LDAPFilter "(SamAccountName=$AffectedUserUserName)" -Properties *

   try{

    $ADUser = Get-ADUser -Filter {SamAccountName -eq $AffectedUserUserName}

   } catch {

    $_ | Out-File -LiteralPath 'e:\logs\errormsg.txt' -Append

   }


This results in this error:

Get-ADUser : The 'Get-ADUser' command was found in the module 'ActiveDirectory', but the module could not be loaded. 

For more information, run 'Import-Module ActiveDirectory'.


I can log into the workflow server as the service account and run these scripts manually and they process correctly without error. The error only occurs when the PSA runs as part of a SR activity workflow.


Any ideas on what could be wrong?

Answers

  • Brian_WiestBrian_Wiest Customer Super IT Monkey ✭✭✭✭✭

    I add this to the top of every PS script. It checks to make sure the module is loaded as part of that session.

    if ( !(Get-Module -Name ActiveDirectory -ErrorAction SilentlyContinue) ) {Import-module ActiveDirectory}
    
  • Nick_FlintNick_Flint Customer Advanced IT Monkey ✭✭✭

    Here's how I was doing it:

    if (-not (Get-Module -Name Import-Module ActiveDirectory)) {

      Import-Module ActiveDirectory -ErrorAction Stop

    }

    I switched over to your code and get the same error.

  • Brian_WiestBrian_Wiest Customer Super IT Monkey ✭✭✭✭✭

    I found this was the only method that allowed the PS scripts to run successfully (at least for my environment)

    Made sure the SCSM Service account has rights to the AD module.

Sign In or Register to comment.