I’m trying to locate users in Active Directory using a function. When I run the code in either ISE or Visual Studio Code, the lookup works. When I run the script in a PowerShell window the lookup doesn’t work. Has anyone seen this before and know what the solution is?
Please provide a sample of what code you’re running and the specific error text you’re getting. Also any other context clues like if you’re running Code/ISE/Ps as a separate account, admin privileges etc.
This happens in both version 5.x and version 7.x. I’m using ISE for version 5.x and VS Code for version 7.x. The lookups work in the ISE and VS Code environments but not when running the script in the PowerShell 5.x or 7.x window. I’m reading a CSV file with user information and attemping to locate them using thier employeeID and then trying with their UPN if the employeeID fails.
Code
Install-Module Active Directory - Global (inside main script)
Function Code
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[array]$user
)
$userID = $user.Employee_Code
$userEmail = $user.Work_Email
$dirUser = Get-ADUser -Filter { EmployeeID -eq $userID } -Properties *
If ($dirUser -ne $null)
{
$global:adUser = $dirUser
}
Else
{
$dirUser = Get-ADUser -Filter { UserPrincipalName -eq $userEmail } -Properties *
If ($dirUser -ne $null)
{
$global:adUser = $dirUser
}
}
Hi, please format your code using the appropriate button from the edit window.
You also didn’t share the error message you’re getting. Your code shows that you’re running Install-Module every time? You shouldn’t have to install the module more than once, or even manually import it. It will be imported automatically when you call a cmdlet from it.
There are no error messages the lookup doesn’t return the user outside the ISE or VS Code. There was a typo I load the module Import-Module ActiveDirectory -Global once in the main body of the script.
The function code
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[array]$user
)
$userID = $user.Employee_Code
$userEmail = $user.Work_Email
$dirUser = Get-ADUser -Filter { EmployeeID -eq $userID } -Properties *
If ($dirUser -ne $null)
{
$global:adUser = $dirUser
}
Else
{
$dirUser = Get-ADUser -Filter { UserPrincipalName -eq $userEmail } -Properties *
If ($dirUser -ne $null)
{
$global:adUser = $dirUser
}
}
Inside the ISE (version 5.x) or VS Code (version 7.x) users are found.
Transscript file top lines processing users from VS Code
The script is now processing the Test.csv file.
The user DAVID AARON was located in the Active Directory
The user RANIA ABBAS could not be located in the Active Directory
Transscript file top lines processing users from PowerShell 7 window.
The script is now processing the Test.csv file.
The user DAVID AARON could not be located in the Active Directory
The user RANIA ABBAS could not be located in the Active Directory
...
Ok, I’m not trying to be rude, but we still don’t know what’s happening here. If it were me I would try copy/pasting line by line what the function is doing into a shell and checking the variables as you go.
Your parameter for your function is $User and says it’s type Array, and then you’re immediately calling properties of that parameter input but we don’t know where that came from.
Also I would advise against setting global variables from your function and instead output an object that you can then capture in your script.
How do you run the code when you run it in a normal console?
And you may share a few lines of your input CSV file as well … !!! formatted as code as well, please !!!
The code works inside the script. I’m using a function as I will need to do this lookup at various times to accomplish different tasks to the users based on added information in the csv file. The purpose of this right now is to make sure everyone in the AD has their proper employee id.
I’m running this as a script calling it with the .\test-script.ps1 command in the PowerShell Window.
Here are the top lines of the CSV
Firstname,Lastname,Position,Employee_Code,Work_Email
DAVID,AARON,Intern,,aarond@graham-windham.org
RANIA,ABBAS,,101495,AbbasR@graham-windham.org
Why don’t you use a function for this purpose as well? And I see you actually don’t output anything with your script. Instead you set a global variable. That’s a bad idea. I’d recommend to make a function of it and simply output the result you expect and use this output in your script.
Agree with both grey0ut and Olaf. Chances are the issue is you are passing multiple items in but processing them as if they were a single item. Your parameter is decorated with [array] which means multiple items. As such, you should process those items in a loop. Even if it’s a single item, being an array with a single item can behave differently than just a single item. I would also recommend not using global scope. If you’re using global, there’s a high probability you’re making things harder on yourself. Next thing, comparison with $null should have $null on the left hand side (LHS). In this example you don’t even need $null, if the variable has anything in it, it will execute the next command. Next, the filter on Get-ADUser should be a string. Powershell is forgiving and in this case converts it to a string and works. Finally, I’d simplify your code by not using so many extraneous and temporary variables. Something like this should work nicely
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[array]$user
)
$userlist = foreach($account in $user){
$dirUser = Get-ADUser -Filter "EmployeeID -eq '$($account.Employee_Code)'" -Properties *
$aduser = if($dirUser){
$dirUser
}
Else{
Get-ADUser -Filter "UserPrincipalName -eq '$($account.Work_Email)'" -Properties *
}
}
When you put the variable in front of a construct such as a foreach loop or if statement, anything that is output inside will collect in the variable for you. In this example, any users found (by first checking employee_code and then work_email if none found) will be collected in the $userlist variable.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.