Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Bulk user updates w/ logging to txt + event log

$
0
0

Hi everyone, 

I'm putting together (using existing examples and some pshell knowledge) a script to update some user attributes on a scheduled basis.

My requirements are:

1. Read csv file with user attributes and update the city & location

2. Log all success/failures to a txt log file with the previous and post-update value for city & location

3. Write any failures to the event log noting the error and any value changes

#3 is the least important of the bunch. Below is what I have accumulated so far but I'm getting jammed up on the logging with previous + current values. 

Users = Import-Csv -Path c:\scripts\import.csv           

Foreach ($user in $users) {            

Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "OU=Accounts,DC=domain,DC=lan" |

Set-ADUser -City $($user.City) -Office $($user.Office)

Write-EventLog -LogName Application -Source "ADUpdate" -EventId 1 -Message "$user.samaccountname was updated to $user.City and $user.Office"

}

Any help would be incredibly appreciated. Thanks in advance!


Viewing all articles
Browse latest Browse all 6937

Trending Articles