Get or set users mobile numbers from active directory in bulk
Today is a double whammy. Here are two scripts to help achieve a single task. In this instance, you have been requested to dump all the user objects mobile numbers and then update the attribute using supplied data. Someone has made an error and supplied “DistinguishedName” in the import file instead of the far simpler “SamAccountName”.
Its important to highlight that while this post focuses on retrieving the mobile numbers of users – this code could be used to capture any AD attributes you require.
Our first code snippet uses the native AD cmdlets to capture the property “mobile” for each user object. This data is then exported to CSV for review (or if required, restoration).
# Import the activeDirectory module - required for PS2 or lower import-module activedirectory # Import our target user list $users = Get-content .\users.txt # Build a report array to capture our data $Report = @() foreach ($user in $users) { # Create a handy error flag $ErrorFlag = $false Try { # Attempt to find the user object and also request the "mobile" property $colUser = get-aduser $user -property mobile } Catch { # If on error, write to the console and change our flag write-host "Error generated: $user" $ErrorFlag = $true } # Create a new object to capture our results $objUserInfo = New-Object System.Object If (!$ErrorFlag) { # When an error is not generated, store the attributes we retrieved $objUserInfo | Add-Member -MemberType NoteProperty -Name SamAccountName -Value $colUser.SamAccountName $objUserInfo | Add-Member -MemberType NoteProperty -Name Mobile -Value $colUser.Mobile } else { # If an error is generated, capture some generic information $objUserInfo | Add-Member -MemberType NoteProperty -Name SamAccountName -Value $user $objUserInfo | Add-Member -MemberType NoteProperty -Name Mobile -Value "Error - user not found" } # Store our new objects in the report array $Report += $objUserInfo } # Export our report to a CSV. $Report | Export-Csv -noType mobiles-export.csv
And here is our second snippet. In this instance, we have been presented with a file containing two parts of information:
DistinguishedName;MobileNumber
As such, we must split the information on the “;” delimiter and work from there.
We will use the handy “set-aduser” cmdlet to apply a change to a default LDAP attribute. The set-aduser cmdlet can be used to alter many of the default LDAP attributes. This provides a great example of how to change user objects in bulk.
In this case, a report is not generated, we would recommend adding a few lines from the above snippet to capture and create a log for audit purposes.
import-module activedirectory # Import our target information $users = get-content .\mobiles-import.txt foreach ($user in $users) { # Once again, create a flag for errors $ErrorFlag = $false # Split up the information we were provided based on ";" $userSplit = $user.split(";") # Store the first part of the split as the user distinguished name $userDN = $userSplit[0] # Store the second part of the split as the user mobile number $userMobile = $userSplit[1] Try { # Attempt to set the users mobile number based on their distinguished name $colUser = set-aduser $userDN -mobilePhone $userMobile write-host "Adding $userMobile to $userDN" } Catch { # Capture errors as required write-host "Error generated: $user" $ErrorFlag = $true } }
And there we have it – how to get or set users mobile numbers from active directory in bulk.