I’m working on a script to make sure certain user information is always correct in AD compared to an external employee database. The one section I’m struggling on to make efficient is making sure each users Address tab (in AD) is properly populated. We has 15 locations and about 1500 users, i’m trying to avoid writing a giant if statement that checks users physicalDeliveryOffice and then verifes if the street, City, Stat Zip, And Country are all correct before trying to change them. Since the Address information isn’t stored anywhere I can pull it from I saved all the locations in an array (mimicking the AD properties) and then looped through and tried to match up the AD properties. But then I realized that I can’t set a variable for an AD property when using –Replace. Is there an easy way to go about this or am I stuck doing a giant if/else if? <BR>An example of how i’m wokring with a location is as such
the loop section I’m trying to accomplish is#Location information for Chicago
$CHI = New-Object PSObject -Property $LocationTableProperties
$CHI.streetAddress=“999 Somewhere`nSuite 666”
$CHI.l=“Chicago”
$CHI.st=“IL”
$CHI.postalcode=“00000”
$CHI.c=“US”
$CHI.PhysicalDeliveryOfficeName=“Chicago”
#Adding to AllLocations list
$AllLocations+=$CHI
#Compare the office location in Sigcontacts agaisnt AD, change in AD if necessary
ForEach ($Location in $AllLocations) {
#Check to see which location matches and use that to compare
If ($ADProperties.PhysicalDeliveryOfficeName -eq $Location.PhysicalDeliveryOfficeName) {
Get-Member -InputObject $Location -MemberType NoteProperty |
ForEach-Object {
#Walk through the location Properties and compare them to the matching AD properites
If ($ADProperties.($.Name) -ne $BAL.($.Name)) {
Write-Verbose “INFO : Employee Address section mismatch: $($Location.PhysicalDeliveryOfficeName) $_ = $($.Name), AD = $($ADProperties.($.Name))”
Try {Set-ADUser $ADProperties.SamAccountName -ErrorAction Stop}
Catch {
Write-Warning “ERROR : Cannot edit $User in AD, moving to the next user”
$ErrorTable.Add($Employee.DomainAccountName,$_.Exception.Message)
break
}
Else {#Write-Host “Good”}
}
Break
}
Else {
Write-Warning “WARNING : $User has no office in AD”
Continue
}
}
Here is the section that isn’t working, am I’m pretty sure it won’t. Just not sure how else to go about it
Set-ADUser $ADProperties.SamAccountName -Replace @{EmployeeID=$Employee.PersonID} -ErrorAction Stop