Reputation: 5
So I'm trying to use PowerShell and a CSV file to populate users street address as well as city, state, zip, etc (in case they move to a new office location their street, city, state, zip will be updated appropriately). Obviously, my OUs are structured by location. My CSV file has 5 columns (Street, Street2, City, State, Zip). Street2 is the suite typically that I want on another line. Although if the zip starts with a zero it gets lost but that's another subject.
Everything works up to line 9 (Get-ADUser
) then starting line 10 (Set-ADUser
) it fails and I think I know why I just haven't figured out how to get past it. I can't use $_
for two different values in the for-eachobject
loop
for Get-ADUser
and Set-ADUser
.
Import-CSV .\Offices.csv |
ForEach-Object {
if($_.Office -eq "Office1"){
$OUStr = "OU=Office1,OU=User Accounts,DC=domain,DC=local"
}elseif($_.Office -eq "Office2"){
$OUStr = "OU=Office2,OU=User Accounts,DC=domain,DC=local"
}
Get-ADUser -Filter "L -eq `"$($_.Office)`"" -SearchBase "$OUStr" -Properties streetAddress,L,st,postalCode |
Set-ADUser -Replace @{
streetAddress="$_.Street" + "`r`n" + "$_.Street2"
L="$_.City"
st="$_.State"
postalCode="$_.Zip"
}
}
Any advice would be appreciated.
Upvotes: 0
Views: 650
Reputation: 61068
It is perhaps easier if you define a hashtable to store all the attributes you want to update and use that for the Set-ADUser
cmdlet.
Using -Replace with LDAP attribute names
$CSV = Import-CSV .\Offices.csv
foreach ($user in $CSV) {
switch ($user.Office) {
"Office1" { $OUStr = "OU=Office1,OU=User Accounts,DC=domain,DC=local"; break }
"Office2" { $OUStr = "OU=Office2,OU=User Accounts,DC=domain,DC=local"; break }
}
# With –Replace you're using the LDAP names of the properties rather than the PowerShell name.
$attribs = @{
streetAddress = "{0}`r`n{1}" -f $user.Street, $user.Street2
l = $user.City
st = $user.State
postalCode = $user.Zip
}
Get-ADUser -Filter "City -eq '$($user.Office)'" -SearchBase $OUStr |
Set-ADUser -Replace $attribs
}
Splatting the parameters using the PowerShell or GUI attribute names
$CSV = Import-CSV .\Offices.csv
foreach ($user in $CSV) {
switch ($user.Office) {
"Office1" { $OUStr = "OU=Office1,OU=User Accounts,DC=domain,DC=local"; break }
"Office2" { $OUStr = "OU=Office2,OU=User Accounts,DC=domain,DC=local"; break }
}
# With splatting you use the PowerShell or GUI names of the properties.
$attribs = @{
StreetAddress = "{0}`r`n{1}" -f $user.Street, $user.Street2
City = $user.City
State = $user.State
PostalCode = $user.Zip
}
Get-ADUser -Filter "City -eq '$($user.Office)'" -SearchBase $OUStr |
Set-ADUser @attribs
}
Hope this helps
Upvotes: 1
Reputation: 25001
I think this will help you:
Import-CSV .\Offices.csv |
ForEach-Object {
if($_.Office -eq "Office1"){
$OUStr = "OU=Office1,OU=User Accounts,DC=domain,DC=local"
}elseif($_.Office -eq "Office2"){
$OUStr = "OU=Office2,OU=User Accounts,DC=domain,DC=local"
}
$streetAddress = $_.street + "`r`n" + $_.street2
$City = $_.City
$state = $_.State
$postalCode = $_.zip
Get-ADUser -Filter "L -eq `"$($_.Office)`"" -SearchBase "$OUStr" -Properties streetAddress,L,st,postalCode |
Set-ADUser -Replace @{
streetAddress=$streetAddress
L=$City
st=$state
postalCode=$postalCode
}
}
I am setting variables for everything you need in the replace hashtable before the Get-Aduser command. This will allow you to reference the object passed into the ForEach
loop.
Upvotes: 0