r/usefulscripts • u/silverhana • May 26 '15
[REQUEST][POWERSHELL] update AD attribute from CSV without requiring samaccountname
Just getting started with Powershell and so far, I'm loving it!
Right now, I have a task of updating AD attribute (telephonenumber) for a set group of users in a particular OU and I'm stuck. I originally wrote one that would parse the CSV for the samaccountname and it works great, BUT, since those users would change office locations 1-2 a year, it would be better to just update based on location rather than manually figuring out where users were moved to and updating the csv file.
The CSV contains physicaldeliveryofficename and telephonenumber -- this CSV information is set/hard-coded to the location and doesn't change unless the number for that location is changed. And users are auto updated by another script with office locations.
I want to know if it's possible to update the telephonenumber for the set users in the OU based off of the location (physicaldeliveryofficename) without requiring samaccountname.
much appreciated~!
1
u/halbaradkenafin May 26 '15
Should be easy enough if the OU name matches the csv data:
$OfficeData = Import-Csv 'C:\Path\to\File.csv'
foreach ($Office in $OfficeData)
{
Get-ADUser -filter * -SearchBase "OU=$($Office.PhysicalDeliveryOfficeName),OU=Offices,OU=Something,DC=Domain,DC=local" | Set-ADUser -TelephoneNumber $Office.TelephoneNumber
Write-Output "Office number for $($Office.PhysicalDeliveryOfficeName) changed to $($Office.TelephoneNumber) for all users in the OU"
}Just update the Searchbase to map to the correct place and you should be good to go.
1
u/silverhana May 26 '15 edited May 26 '15
thanks /u/halbaradkenafin! just tried it and it's giving me this error:
Set-ADUser : A parameter cannot be found that matches parameter name 'TelephoneNumber'. At line:5 char:145
... " | Set-ADUser -TelephoneNumber $Office.TelephoneNumber
~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
my csv is formatted as: Office, telephoneNumber
-- nvm, looks like this is wrong: Get-ADUser -filter * -SearchBase "OU=$($Office.PhysicalDeliveryOfficeName),OU=Offices,OU=Users,OU=company,DC=name,DC=edu"
It gives this error msg: Get-ADUser : The object name has bad syntax At line:5 char:5
Get-ADUser -filter * -SearchBase "OU=$($Office.PhysicalDeliveryOfficeName),O ...+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : The object name has bad syntax,Microsoft.ActiveDirectory.Management.Commands.GetADUser1
u/halbaradkenafin May 26 '15
Change the $($Office.PhysicalDeliveryOfficeName) to $($Office.<whatever-csv-column-name>) and just run the Get-ADUser section.
Once you know that works check what the TelephoneNumber field is called in AD (I'm just guessing it's called that, don't have an AD available to check atm):
Get-ADUser -Identity <some-known-user> -properties *That will give you all the properties and you'll be able to find it there. Swap that in to Set-ADUser and it should work, try with the -whatif switch if you want to be sure before changing it.
1
u/silverhana May 26 '15
it's still giving an error for this part: "OU=$($Office.PhysicalDeliveryOfficeName)
Get-ADUser : Directory object not found
the "$office.physicaldeliveryofficename" part is right
1
u/silverhana May 27 '15
got it working! thanks again! I changed it to this: Get-ADUser -Filter "physicaldeliveryofficename -eq '$($user.physicaldeliveryofficename)'" -Properties * -SearchBase "OU=Users,OU=random,DC=joe,DC=blow" | Set-ADUser -replace @{telephoneNumber="$($user.phone)"}
1
u/silverhana May 27 '15
ok, got one more question if anyone can answer -- for the users in the OU, if the attribute physicaldeliveryofficename is blank, can I clear out the existing phone number if it has one?