Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [int64]$finalcount = 0
- $userlist = get-aduser ctxtest2 -properties ipPhone,telephonenumber
- #FOREACH LOOP TO PERFORM THE PHONE NUMBER FORMATTING ON EACH AD ACCOUNT AND SET THE OFFICEPHONE PROPERTY
- foreach($user in $userlist)
- {
- #RESET VARIABLES TO NULL
- #$ipphone = $null
- #$telephone = $null
- #$pnum = $null
- #$ipphone = $null
- #SET THE MAIN VARIABLES
- $guid = $user.objectGUID
- $ipphone = $user.ipPhone
- $ipphonelist = $user.telephonenumber
- $finalcount = $finalcount + 1
- #REMOVE FORMATTING FROM IPPHONE AND TELEPHONE AND COMPARE THE TWO
- if($ipphonelist)
- {
- $ipphonelist = $ipphonelist -replace "[^0-9]",""
- $ipphonelist = $ipphonelist.Substring(0)
- }
- #CONVERT $ipphonelist FROM STRING ARRAY TO STRING
- foreach($thing in $ipphonelist)
- {
- $pnum = $thing
- }
- #IF THERE IS NO TELEPHONENUMBER, THEN SKIP THE REST OF THE SCRIPT AND CONTINUE
- if(-not $pnum)
- {
- Write-Host "Phone does not exist for: "$user.name" --- Total number of accounts worked on:" $finalcount
- continue
- }
- if($ipphone -match $null)
- {
- Write-Host "IPPhone for "$user.name" is NULL"
- continue
- }
- if($ipphone -match 9999999999)
- {
- Write-Host "User: "$user.name"--- IPPhone: $ipphone"
- continue
- }
- #REFORMAT $pnum - REMOVE FORMATTING
- $pnum = $pnum -replace "[^0-9]",""
- $pnumlength = $pnum.length
- if($pnumlength -ge '11')
- {
- $pnum = $pnum.Substring(0,10)
- }
- elseif($pnumlength -eq 10)
- {
- #DO NOTHING, THIS IS CORRECT
- }
- write-host $user.Name Phone is $pnum --- Trying to set iphone to $pnum
- Set-ADUser -Identity $guid -Replace @{'ipPhone'=$pnum} -ErrorVariable pnumerror
- If ($pnumerror) {
- Write-Warning -Message 'Could not set';
- }
- else
- {
- Write-Host $user.Name IPPhone set to IPPhone: $pnum
- }
- write-host Total number of accounts worked on: $finalcount
- $ipphone = $null
- $telephone = $null
- $pnum = $null
- $ipphone = $null
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement