Reputation: 115
I am writing a code to update an Active Directory using input from a CSV file. I am able to get the script to run all of the way to the end, but when I check the Active Directory, it is not updated. When I check the log file, it does not list any CSV file as being used. Everything is in the same folder and the log file writes fine.
I have tried altering the path that the script searches for the CSV file to C:/PATH/TO/FILE/filename I have also tried with quotes, without quotes, with -Path, and without -Path. I have looked at the other StackOverflow articles and they all seem to say that this should work... I think the key line here is the 3rd from the top. "$Users = Import-csv -Path users.csv" I could be wrong though.
Import-Module ActiveDirectory
#Import and Specify CSV File
$Users = Import-csv -Path users.csv
#Specify Log File
$LogFile = ".\UpdateUsersCsv.log"
# Write to the log file. Abort the script if the log file cannot be updated.
Add-Content -Path $LogFile -Value "================================================"
Add-Content -Path $LogFile -Value "UpdateUsersCsv.ps1"
Add-Content -Path $LogFile -Value "Update Users from CSV file: $Users"
Add-Content -Path $LogFile -Value "Started: $((Get-Date).ToString())"
Add-Content -Path $LogFile -Value "Log file: $LogFile"
Add-Content -Path $LogFile -Value "------------------------------------------------"
# Initialize counters.
$Updated = 0
$NotFound = 0
#Find Users
foreach($User in $Users){
$ADUser = Get-ADUser
#If found, update Users and increment $Updated
If($ADUser){
Set-ADUser $User.SamAccountName -Description $User.NewDescription
$Updated = $Updated + 1
#If not found, increment $NotFound
}ElseIf(!$ADUser){
Add-Content -Path $LogFile -Value "User $ASUser not found."
$NotFound = NotFound + 1
}
}
Add-Content -Path $LogFile -Value "Number of users updated: $Updated"
Add-Content -Path $LogFile -Value "Number of users not found: $NotFound"
Add-Content -Path $LogFile -Value "================================================"
Write-Host "Script Completed, check log file $LogFile for more information."
The Active directory should be updated with whatever information is in the CSV file. For example, if I used : myName, myDescription goes here It should update the description of the user called "myName" to say "myDescription goes here" Then the log file should say something along the lines of: From csv file: nameOfCSV Users updated = 1 Users not found = 0
If the user is not found, it should report that accordingly, but that is not my current problem.
When I run the script and open the log file, there is no csv file shown, and the users updated and users not found fields are left at 0. This leads me to believe that the problem is that the csv file is not importing.
Upvotes: 0
Views: 1212
Reputation: 2676
I see what the problem is. Your code will only update the AD, if the user is found. The command to look for an AD user is Get-Aduser -identity <SamAccountName>
In your code, you have simply used $ADUser = Get-ADUser
without any parameters. I am surprised why you do not get any errors or prompt that asks for -identity
parameter value.
In your csv, you probably have a column like this.
Then in your foreach loop would change like
foreach($User in $Users){
$ADUser = Get-ADUser $User.UserID
...
}
Now, if the user exists, your code will execute the Set-AdUser
block. In your current code, since it can't actually find the user, $Aduser
would presumably (still surprised it gets that far without prompting for Identity) be empty and would always hit the ElseIf
block.
Upvotes: 2