Error Using ActiveDirectory PowerShell Module
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
I add this to the top of every PS script. It checks to make sure the module is loaded as part of that session.
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.
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.
Hi @Nick_Flint , we are getting the same error but with VSTeam-Module instead. Ever found a working solution to this?
@Thorsten_Mewes Unfortunately not. I had to resort to other methods to achieve what I was trying to do.
Hey @Thorsten_Mewes and/or @Nick_Flint. Could you share a bit more about:
I just did a quick test again of running the AD module within an SR and managed to interact with Get-AD User/Computer/Group as expected. I'm running: