ADScripts/Normalize-PhoneNumbers.ps1

59 lines
2.6 KiB
PowerShell
Raw Normal View History

2023-09-14 11:13:58 -06:00
# Script to normalize Phone Number format to (555) 555-5555 format for AD Users.
# If telephoneNumber empty, add Main Office Line based on Office Attribute
# Teams Extension to be added to ipPhone attribute in AD.
Import-Module ActiveDirectory
<#Office Phone Numbers#>
$lethbridgeNumber = "4033293442"
$calgaryNumber = "4032501362"
$medicineHatNumber = "4035482626"
$redDeerNumber = "4033488340"
$edmontonNumber = "7804862000"
$saskatoonNumber = "3066681966"
$reginaNumber = "3065220010"
$winnipegNumber = "2046883805"
$vancouverNumber = "7782318880"
$naniamoNumber = "2502685008"
$grandePrairieNumber = "5877713239"
$RegexToRemove = 'boardroom|board|room|presentation|present|conference|tablet|Yingfeng|draftingdirector|og-|IT|survey|quality|safety|tstormie|Taylor Stormie|Ron Hust'
$cleanPhoneNumber = ""
<#Set the format for the phone number by moving brackets, spaces or dashes around. Dont touch $1, $2, $3#>
$numberFormat = '($1) $2-$3'
# gets AD Creds for update operations
if (!$creds) { $creds = Get-Credential }
$list = Get-ADUser -Credential $creds -server lbdc01.mpe.ca -SearchBase "OU=MPEUsers,DC=mpe,DC=ca " -Filter * -Properties telephoneNumber, title, DisplayName, office | where -Property DisplayName -NotMatch $RegexToRemove
foreach ($person in $list) {
if (!$person.telephoneNumber) {
switch ($person.office) {
"Lethbridge" { $cleanPhoneNumber = $lethbridgeNumber }
"Calgary" { $cleanPhoneNumber = $calgaryNumber }
"Medicine Hat" { $cleanPhoneNumber = $medicineHatNumber }
"Red Deer" { $cleanPhoneNumber = $redDeerNumber }
"Edmonton" { $cleanPhoneNumber = $EdmontonNumber }
"Saskatoon" { $cleanPhoneNumber = $saskatoonNumber }
"Regina" { $cleanPhoneNumber = $reginaNumber }
"Winnipeg" { $cleanPhoneNumber = $winnipegNumber }
"Vancouver" { $cleanPhoneNumber = $vancouverNumber }
"Naniamo" { $cleanPhoneNumber = $NaniamoNumber }
"Grande Prairie" { $cleanPhoneNumber = $GrandePrairieNumber }
"Lethbridge North" { $cleanPhoneNumber = $lethbridgeNumber }
}
}
else {
$cleanPhoneNumber = $person.telephoneNumber -replace "[^0-9]"
} if (($cleanPhoneNumber.Length) -gt 10) {
# check out Candy Stang's data - may need to adjust logic, or decide how to handle it)
$cleanPhoneNumber = $cleanPhoneNumber.Substring($cleanPhoneNumber.Length - 10)
}
$formattedNumber = $cleanPhoneNumber -replace '(\d{3})(\d{3})(\d{4})', $numberFormat
$person | Set-ADUser -Credential $creds -Server lbdc01.mpe.ca -OfficePhone $formattedNumber <#-Replace @{ipPhone="5000"}#>
}