PoshCode Archive  Artifact Content

Artifact 4f3209403ee032679a682a5703b1ec63f7418d197d83f19260b6aada6f66379c:

  • File Update-phone-in-AD.ps1 — part of check-in [6ba7345604] at 2018-06-10 13:18:22 on branch trunk — Strips off the ’1-’ prefix from phone numbers stored in Active Directory. (user: David Retherford size: 1670)

# encoding: ascii
# api: powershell
# title: Update phone # in AD
# description: Strips off the ’1-’ prefix from phone numbers stored in Active Directory.
# version: 0.1
# author: David Retherford
# license: CC0
# x-poshcode-id: 2992
# x-archived: 2012-01-14T07:03:40
# x-published: 2012-10-07T06:28:00
#
# Requires ‘ActiveRoles Management Shell for Active Directory’ from Quest Software.
#
#------------------------------------------------------------------------------
# Strips off the '1-' prefix from phone numbers stored in Active Directory.
# Requires 'ActiveRoles Management Shell for Active Directory' from Quest
# Software.
#------------------------------------------------------------------------------
$data = @()
$users = Get-QADUser -sizelimit 0 `
    | Where-Object { $_.PhoneNumber -like "1-*" } `
    | Where-Object { $_.PhoneNumber.Length -eq 14 } `
    | Sort-Object -Property Name
foreach ( $user in $users )
{
    if ( $user -eq $null )
    {
        continue
    }
    $newPhoneNumber = $user.PhoneNumber.ToString()
    $newPhoneNumber = $newPhoneNumber.Substring(2,12)
    $dataLine = New-Object psObject
    $dataLine | Add-Member -MemberType NoteProperty -Name "User" -Value $user.Name
    $dataLine | Add-Member -MemberType NoteProperty -Name "OldPhone" -Value $user.PhoneNumber
    $dataLine | Add-Member -MemberType NoteProperty -Name "NewPhone" -Value $newPhoneNumber
    $dataLine | Add-Member -MemberType NoteProperty -Name "DN" -Value $user.DN
    $data += $dataLine
    Set-QADUser -Identity $user.DN -PhoneNumber $newPhoneNumber 
}
if ( $data.Count -gt 0 )
{
    $data | Out-GridView
}