Set Users Telephone tab

On the user properties dialog there is a tab called Telephones. This enables us to set phones numbers – primary & others for:

  • Home
  • Pager
  • Mobile
  • Fax
  • IPphone

There is also a Notes box which we’ll look at separately

Instead of doing a single script for each type of phone I decided to use some of the parameterset and switch logic to produce just one script

function set-TelephoneTab {            
[CmdletBinding(SupportsShouldProcess=$true)]             
param (             
[parameter(Mandatory=$true)]              
 [ValidateNotNullOrEmpty()]            
 [string]$name,            
            
 [ValidateNotNullOrEmpty()]            
 [string]$phonenumber,            
             
 [ValidateNotNullOrEmpty()]            
 [string[]]$otherphonenumber,            
             
[parameter(ParameterSetName="Home")]            
 [switch]$homephone,            
             
[parameter(ParameterSetName="Pager")]            
 [switch]$pager,            
             
[parameter(ParameterSetName="Mobile")]            
 [switch]$mobile,            
             
[parameter(ParameterSetName="Fax")]            
 [switch]$fax,            
             
[parameter(ParameterSetName="IP")]            
 [switch]$ip,            
             
 [ValidateSet("M", "P", "Q", "S")]            
 [string]$type = "S"            
             
)            
if ($phonenumber) {            
switch ($psCmdlet.ParameterSetName) {            
 "Home"   {$primary = "homePhone" }            
 "Pager"  {$primary = "pager" }            
 "Mobile" {$primary = "mobile" }            
 "Fax"    {$primary = "facsimileTelephoneNumber" }            
 "IP"     {$primary = "ipPhone" }            
 default {Write-Host "Error!!! Should not be here" }            
}            
}            
            
if ($otherphonenumber) {            
switch ($psCmdlet.ParameterSetName) {            
 "Home"   {$other = "otherHomePhone" }            
 "Pager"  {$other = "otherPager" }            
 "Mobile" {$other = "otherMobile" }            
 "Fax"    {$other = "otherFacsimileTelephoneNumber" }            
 "IP"     {$other = "otherIpPhone" }            
 default {Write-Host "Error!!! Should not be here" }            
}            
}            
            
switch ($type) {            
#Microsoft            
"M"  {              
       if ($phonenumber) {            
         Get-ADUser -Identity $name |            
         Set-ADUser -Replace @{$primary = $phonenumber}            
       }            
       if ($otherphonenumber) {            
         Get-ADUser -Identity $name |            
         Set-ADUser -Replace @{$other = $otherphonenumber}            
       }            
     }            
#AD provider            
"P" {            
       if ($phonenumber) {            
        Set-ItemProperty -Path AD:\$name  -Name $primary -Value $phonenumber  -Force            
      }              
      if ($otherphonenumber) {            
        Set-ItemProperty -Path AD:\$name  -Name $other -Value $otherphonenumber  -Force            
      }              
    }             
#Quest            
"Q" {            
       if ($phonenumber) {            
        Get-QADUser -Identity $name |            
        Set-QADUser -ObjectAttributes @{$primary= $phonenumber}            
       }             
       if ($otherphonenumber) {            
        Get-QADUser -Identity $name |            
        Set-QADUser -ObjectAttributes @{$other = $otherphonenumber}            
       }             
                   
    }            
#Script            
"S" {            
       if ($phonenumber) {            
        $user = [adsi]"LDAP://$name"            
        $user."$primary" = $phonenumber            
        $user.SetInfo()             
       }             
       if ($otherphonenumber) {            
        $user = [adsi]"LDAP://$name"            
        $user."$other" = $otherphonenumber            
        $user.SetInfo()             
       }             
     }            
default {Write-Host "Error!!! Should not be here" }                 
} ## end of type switch            
            
}  ## end of function

This isn’t as complicated as it looks.

The function expects a number of parameters:

  • name – I would use the distinguished name unless you explicitly force the use of Microsoft or Quest cmdlets
  • phone number or otherphonenumber  -the first is a single string the second is an array of numbers
  • a set of switches to determine which type of phone – they are in different parameter sets to force mutual exclusivity
  • a type parameter which forces the use of cmdlets, provider or script – the default is script

if phonenumber and/or otherphonenumber have values the appropriate attribute name is set

the last switch statement determines the type (cmdlet, provider or script) and executes the changes

Some examples should make it clear how it works.

set-TelephoneTab -name usera -phonenumber “123456” -homephone -type “M”
set-TelephoneTab -name userc -phonenumber “123456” -mobile -type “Q”

set-TelephoneTab -name “CN=UserB,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “123456” -pager -type “P”

set-TelephoneTab -name “CN=UserD,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “123456” -fax

set-TelephoneTab -name “CN=UserD,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “1234567” -ip -type “S” 

 

set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “012345” -ip -type “M”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “0123456” -mobile -type “P”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “01234567” -fax  -type “Q”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “012345678” -pager   -type “S”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -phonenumber “0123456789” -homephone

 

$otherh = “01234 5667890”, “01234 5667880”, “01234 5667770”, “01234 5667660”
$otherp = “01234 5667891”, “01234 5667881”, “01234 5667771”, “01234 5667661”
$otherf = “01234 5667892”, “01234 5667882”, “01234 5667772”, “01234 5667662”
$otheri = “01234 5667893”, “01234 5667883”, “01234 5667773”, “01234 5667663”
$otherm = “01234 5667894”, “01234 5667884”, “01234 5667774”, “01234 5667664”   

set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -otherphonenumber $otheri -ip -type “M”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -otherphonenumber $otherm -mobile -type “P”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -otherphonenumber $otherf -fax  -type “Q”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -otherphonenumber $otherp -pager   -type “S”
set-TelephoneTab -name “CN=UserA,OU=BlogTests,DC=Manticore,DC=org” -otherphonenumber $otherh -homephone

Advertisements
This entry was posted in PowerShell and Active Directory. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s