Add Phone Numbers to existing AD Contacts

Occasional Visitor

Dear Community

Within the last Days, i had to import ~36000 AD Contacts from Excel. I came from basically zero knowledge and created (copy/paste/edit) following:

 

Contact Import

 

 

function GetUserAttributes($ht)
{
    $otherAttributes = @{}
    foreach ($k in $ht.Keys)
    {
         if(-not [System.String]::IsNullOrWhiteSpace($ht[$k]))
         {
            $otherAttributes.Add($k,$ht[$k])
         }
    }
    $otherAttributes
}
##function CutLeadTrailBlank(
$Users = Import-XLSX -Path "\\server\it\Gal_export.xlsx"
foreach ($user in $Users)
{
    $Displayname =  $user.'Givenname' + ' ' + $user.'surname'
    $Displayname = $Displayname.Trim()

    $ht = @{
            sn = $user.'Surname'
            givenName = $user.'GivenName'
            Displayname = $Displayname
            mail = $user.'emailaddress'
            PostalCode = $User.'postalcode'
            physicalDeliveryOfficeName = $User.'Office'
            streetAddress = $User.'Streetaddress'
            l = $User.'City'
            st = $User.'State'
            c = $User.'Country'
            department = $User.'Department'
            Company = $User.'Company'
            ##'msDS-SourceObjectDN' = 'OU=Unit3,OU=Unit2D,OU=Unit1,DC=Domain,DC=local'
         }

            
    $otherAttributes = GetUserAttributes($ht)
    ##$password = $user.Password | ConvertTo-SecureString -AsPlainText -Force
    if($otherAttributes.Count -gt 0)
    
    {
       New-ADObject -Type contact -name $user.Name -Path 'OU=Unit3,OU=Unit2D,OU=Unit1,DC=Domain,DC=local' -OtherAttributes $otherAttributes
    }
}

 

 

 

 

 

Enable Mail Contact:

 

 

 

 

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;


$Users = Get-ADObject -LDAPFilter "(objectClass=contact)" -SearchBase 'OU=Unit3,OU=Unit2,OU=Unit1,DC=Domain,DC=local' -Properties Name,Mail

ForEach($User in $Users)
        {
        
        $Params1 = @{

        Identity = $User.'Name'
        ExternalEmailAddress = $User.'Mail'
        }
    
    Enable-MailContact @Params1

}

 

 

 

 

 

Disable EmailAdressPolicy and replace Adresses

 

 

 

 

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;

$Users = Get-MailContact -ResultSize unlimited -organizationalunit 'Domain.local/Unit1/Unit2/Unit3'

ForEach($User in $Users)
        {
        
        $Params2 = @{

        Identity = $User.'Name'
        EmailAddressPolicyEnabled = $false
        EmailAddresses = $User.'ExternalEmailAddress'
        }

    Set-Mailcontact @Params2
}

 

 

 

 

I am pretty sure that everything could be done nicer and in one Script, but that worked well.

 

Now i got a new File with just MailAdresses, telephone and Mobilenumbers that i have to add. How to start? My Idea is to read the contacts, read the file, look for corresponding mail addresses and set the phone numbers. How do I find the corresponding contact in AD that matches the mail address from the File?

 

Any Help appreciated!

Thanks, Dani

 

1 Reply

@vision311 Hello, Dani. 

You can adapt this snippet to your requirements. 

$ADContacts   = Get-MailContact -ResultSize unlimited -OrganizationalUnit 'Domain.local/Unit1/Unit2/Unit3'
$PhoneNumbers = Import-Csv "C:\Users\emoreno\Desktop\Gal_Export.csv" #Headers: MailAdresses | Telephone | Mobilenumber

Foreach($ToSet in $PhoneNumbers)
 {
  $ContactMatch? = $null
  $ContactMatch? = $ADContacts | Where-Object{$ToSet.MailAdresses -like "*$($_.PrimarySmtpAddress)*" } 
  
  If($ContactMatch?)
   {
    Write-Host "Match found for $($($ContactMatch?.PrimarySmtpAddress).Address)" -ForegroundColor Cyan
    Set-Contact -Identity $($ContactMatch?.Identity) -Phone $ToSet.Telephone
    Set-Contact -Identity $($ContactMatch?.Identity) -OtherTelephone $ToSet.Mobilenumber
   }
  Else
   {
    Write-Host "No Mail Address Match for $($ToSet.MailAddresses)" -ForegroundColor Yellow
   }
 }

 

Regards

Erick Moreno