So I've got a CSV File with the following format to bulk edit the Pager field in active directory and I can't seem to figure out why it's erroring out.
SamAccountName Pager
User1,85098838
User2,85939379
User3,85738299
Here's the script:
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the path to the CSV file
$csvFilePath = "C:\pager.csv"
# Read the CSV file
$users = Import-Csv $csvFilePath
# Loop through each user in the CSV and update the Pager field
foreach ($user in $users) {
$samAccountName = $user.SamAccountName
$pagerValue = $user.Pager
# Get the user object from Active Directory
$userObj = Get-ADUser -Filter {SamAccountName -eq $samAccountName}
if ($userObj) {
# Update the Pager field
Set-ADUser -Identity $userObj -Pager $pagerValue
Write-Host "Updated Pager for user $samAccountName with value: $pagerValue"
} else {
Write-Host "User $samAccountName not found in Active Directory."
}
}
And here's the error I get:
Get-ADUser : Variable: 'samAccountName' found in expression: $samAccountName is not defined.
At line:9 char:16
+ ... $userObj = Get-ADUser -Filter {SamAccountName -eq $samAccountName}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
User not found in Active Directory.
Any ideas on what I'm missing?
I was expecting it to work and update the AD field pager