Advertisement
Guest User

IpPhone

a guest
Nov 13th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [int64]$finalcount = 0
  2. $userlist = get-aduser ctxtest2 -properties ipPhone,telephonenumber
  3. #FOREACH LOOP TO PERFORM THE PHONE NUMBER FORMATTING ON EACH AD ACCOUNT AND SET THE OFFICEPHONE PROPERTY
  4. foreach($user in $userlist)
  5. {
  6.     #RESET VARIABLES TO NULL
  7.     #$ipphone = $null
  8.     #$telephone = $null
  9.     #$pnum = $null
  10.     #$ipphone = $null
  11.  
  12.     #SET THE MAIN VARIABLES
  13.     $guid = $user.objectGUID
  14.     $ipphone = $user.ipPhone
  15.     $ipphonelist = $user.telephonenumber
  16.     $finalcount = $finalcount + 1
  17.     #REMOVE FORMATTING FROM IPPHONE AND TELEPHONE AND COMPARE THE TWO
  18.     if($ipphonelist)
  19.     {
  20.         $ipphonelist = $ipphonelist -replace "[^0-9]",""
  21.         $ipphonelist = $ipphonelist.Substring(0)
  22.     }
  23.     #CONVERT $ipphonelist FROM STRING ARRAY TO STRING
  24.     foreach($thing in $ipphonelist)
  25.     {
  26.         $pnum = $thing
  27.     }
  28.     #IF THERE IS NO TELEPHONENUMBER, THEN SKIP THE REST OF THE SCRIPT AND CONTINUE
  29.     if(-not $pnum)
  30.     {
  31.         Write-Host "Phone does not exist for: "$user.name" --- Total number of accounts worked on:" $finalcount
  32.         continue
  33.     }
  34.      if($ipphone -match $null)
  35.     {
  36.         Write-Host "IPPhone for "$user.name" is NULL"
  37.         continue
  38.     }
  39.     if($ipphone -match 9999999999)
  40.     {
  41.         Write-Host "User: "$user.name"--- IPPhone: $ipphone"
  42.         continue
  43.     }
  44.     #REFORMAT $pnum - REMOVE FORMATTING
  45.     $pnum = $pnum -replace "[^0-9]",""
  46.     $pnumlength = $pnum.length
  47.     if($pnumlength -ge '11')
  48.     {
  49.         $pnum = $pnum.Substring(0,10)
  50.     }
  51.     elseif($pnumlength -eq 10)
  52.     {
  53.         #DO NOTHING, THIS IS CORRECT
  54.     }
  55.    write-host $user.Name Phone is $pnum --- Trying to set iphone to $pnum
  56.    Set-ADUser -Identity $guid -Replace @{'ipPhone'=$pnum} -ErrorVariable pnumerror
  57.    If ($pnumerror) {
  58.    Write-Warning -Message 'Could not set';
  59.    }
  60.    else
  61.    {
  62.    Write-Host $user.Name IPPhone set to IPPhone: $pnum
  63.    }
  64.    write-host Total number of accounts worked on: $finalcount
  65.    $ipphone = $null
  66.     $telephone = $null
  67.     $pnum = $null
  68.     $ipphone = $null
  69.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement