Skip to content

Instantly share code, notes, and snippets.

@zplume
Created June 26, 2013 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zplume/5865633 to your computer and use it in GitHub Desktop.
Save zplume/5865633 to your computer and use it in GitHub Desktop.
Update the mobile number and work number values (from Active Directory) for all users in the User Information List, for all site collections in the specified web application.
##### ABOUT #####
# This script updates the mobile number and work number values
# for all users in the "User Information List" for all site collections
# in the specified web application (see variables below)
##### VARIABLES #####
$webAppURL = "http://mywebapp"
$logPath = "C:\temp\UpdateUserInfo\UpdateUserInfo"
##### FUNCTIONS #####
Function GetUserInfo($web) {
$list = $web.Lists["User Information List"]
$items = $list.Items
return $items
}
Function UpdateUserInfoFromAD($log, $userInformationItem, $loginName, $spMobileNo, $spWorkNo) {
# get the username from the login name (remove "DOMAIN\")
$username = $loginName.Split("\")[1]
# get the user object from AD
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$username'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
# track whether or not the item was updated
$itemUpdated = $false
$mobileNo = $user.DS_mobile
if(![string]::IsNullOrEmpty($mobileNo)) {
if($mobileNo -ne $spMobileNo) {
Write-Host -foregroundcolor Green "Mobile number in AD is" $mobileNo
$userInformationItem["MobilePhone"] = $mobileNo # update
$itemUpdated = $true
}
} else {
Write-Host -foregroundcolor Red "Mobile number not found in AD"
}
$log += $($mobileNo + "`t")
$workNo = $user.DS_telephoneNumber
if(![string]::IsNullOrEmpty($workNo)) {
if($workNo -ne $spWorkNo) {
Write-Host -foregroundcolor Green "Work number in AD is" $workNo
$userInformationItem["WorkPhone"] = $workNo # update
$itemUpdated = $true
}
} else {
Write-Host -foregroundcolor Red "Work number not found in AD"
}
$log += $($workNo + "`t")
if($itemUpdated) {
$userInformationItem.SystemUpdate() # save the changes
}
$log += $($itemUpdated.ToString() + "`r`n")
return $log
}
##### SCRIPT START ######
$webApp = Get-SPWebApplication $webAppURL
foreach($site in $webApp.Sites) {
$web = $site.RootWeb
$web.AllowUnsafeUpdates = $true
# get last bit of URL, store in $webName variable (used to name the log file)
$urlParts = $web.Url.Split("/")
[System.Array]::Reverse($urlParts)
$webName = $urlParts[0]
# create the log variable for this site collection (tab separated values file), set the headers
$log = "Site`tUser`tSPMobile`tSPWork`tADMobile`tADWork`tItemUpdated`r`n";
Write-Host -foregroundcolor White $web.Url
$items = GetUserInfo $web
# create error log variable (tab separated values file), set the headers
$errLog = "Site`tUser`tError`r`n"
# create a flag to determine whether or not any errors occured for this site collection
$errorsLogged = $false
foreach($item in $items) {
$userInfo = [xml]$item.Xml # get the Xml
try {
# skip groups
if($userInfo.row.ows_ContentType -eq "Person") {
$log += $($web.Url + "`t") # log web URL
$log += $($userInfo.row.ows_Name + "`t") # log user login name
# get mobile number
$spMobileNo = $userInfo.row.ows_MobilePhone
# get work number
$spWorkNo = $userInfo.row.ows_WorkPhone
$log += $($spMobileNo + "`t") # log the MobileNumber value in the User Information List
$log += $($spWorkNo + "`t") # log the WorkNumber value in the User Information List
# Update the User Information List item with any changes from AD and add the values in AD to the log
$log = UpdateUserInfoFromAD $log $item $userInfo.row.ows_Name $spMobileNo $spWorkNo
}
} catch [Exception] {
# log site collection, user, and error message
$errLog += $($web.Url + "`t" + $userInfo.row.ows_Name + "`t" + $_.Exception.Message + "`r`n")
$errorsLogged = $true # update the error flag so that we know to save the errors to a log file
}
}
if($errorsLogged) {
$errLog | Out-File $($logPath + "\ERROR_" + $webName + ".csv") # save the error log
}
$log | Out-File $($logPath + "\" + $webName + ".csv") # save the log file
$web.Dispose()
$site.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment