Wednesday, June 18, 2014

Exchange 2010 GAL vcard export script

To round out the Vcard export scripts as many people are still running Exchange 2010 here's a Remote powershell script that will allow you to export Mailboxes (or contacts) from Active directory to Vcards and also include the GAL Photo from the AD thumbnail property if set.

 The script uses the Get-User Exchange Management Shell cmdlet to get all the Mailboxes details to include in the Vcard file and also use LDAP to read the AD thumbnailPhoto (if its been set).

before you run this script make sure you set the following variable to the directory you want the vcards exported to

$exportFolder = "c:\temp\"

You need to run this script from within the Exchange Management Shell or a Remote Powershell session

I've put a download of this script here the code looks like

  1. $exportFolder = "c:\temp\" 
  2. $Mailboxes = Get-User -RecipientTypeDetails UserMailbox  
  3. foreach($Mailbox in $Mailboxes){ 
  4.     try{     
  5.          
  6.         $DisplayName = $Mailbox.DisplayName; 
  7.         write-host("Processing " + $DisplayName) 
  8.         $fileName =  $exportFolder + $DisplayName + "-" + [Guid]::NewGuid().ToString() + ".vcf" 
  9.         add-content -path $filename "BEGIN:VCARD" 
  10.         add-content -path $filename "VERSION:2.1" 
  11.         $givenName = $Mailbox.FirstName 
  12.         $surname = $Mailbox.LastName 
  13.         add-content -path $filename ("N:" + $surname + ";" + $givenName) 
  14.         add-content -path $filename ("FN:" + $Mailbox.DisplayName) 
  15.         $Department = $Mailbox.Department; 
  16.         add-content -path $filename ("EMAIL;PREF;INTERNET:" + $Mailbox.WindowsEmailAddress) 
  17.         $CompanyName = $Mailbox.Company 
  18.         add-content -path $filename ("ORG:" + $CompanyName + ";" + $Department)  
  19.         if($Mailbox.Title -ne ""){ 
  20.             add-content -path $filename ("TITLE:" + $Mailbox.Title) 
  21.         } 
  22.         $Country = "" 
  23.         $City = "" 
  24.         $Street = "" 
  25.         $State = "" 
  26.         $PCode = "" 
  27.         if($Mailbox.City -ne ""){ 
  28.             $City = $Mailbox.City 
  29.         } 
  30.         if($Mailbox.StateOrProvince -ne ""){ 
  31.             $State = $Mailbox.StateOrProvince 
  32.         } 
  33.         if($Mailbox.StreetAddress -ne ""){ 
  34.             $Street = $Mailbox.StreetAddress 
  35.         } 
  36.         if($Mailbox.CountryOrRegion -ne ""){ 
  37.             $Country = $Mailbox.CountryOrRegion 
  38.         } 
  39.         if($Mailbox.PostalCode -ne ""){ 
  40.             $PCode = $Mailbox.PostalCode 
  41.         } 
  42.         $addr =  "ADR;WORK;PREF:;" + $Country + ";" + $Street + ";" +$City + ";" + $State + ";" + $PCode + ";" + $Country 
  43.         add-content -path $filename $addr 
  44.         if($Mailbox.MobilePhone -ne ""){ 
  45.             add-content -path $filename ("TEL;CELL;VOICE:" + $Mailbox.MobilePhone)                   
  46.         } 
  47.         if($Mailbox.Phone -ne ""){ 
  48.             add-content -path $filename ("TEL;WORK;VOICE:" + $Mailbox.Phone) 
  49.         } 
  50.         if($Mailbox.Fax -ne ""){ 
  51.             add-content -path $filename ("TEL;WORK;FAX:" + $Mailbox.Fax) 
  52.         } 
  53.         if($Mailbox.HomePhone -ne ""){ 
  54.             add-content -path $filename ("TEL;HOME;VOICE:" + $Mailbox.HomePhone) 
  55.         } 
  56.         if($Mailbox.WebPage -ne ""){ 
  57.             add-content -path $filename ("URL;WORK:" + $Mailbox.WebPage) 
  58.         } 
  59.         Try{ 
  60.                 $sidbind = "LDAP://<SID=" + $Mailbox.Sid + ">" 
  61.                 $userObj = [ADSI]$sidbind 
  62.                 $photo = $userObj.thumbnailPhoto.value 
  63.                 if($photo -eq $null){"No Photo"} 
  64.                 if($photo -ne $null){ 
  65.                     add-content -path $filename "PHOTO;ENCODING=BASE64;TYPE=JPEG:" 
  66.                     $ImageString = [System.Convert]::ToBase64String($photo,[System.Base64FormattingOptions]::InsertLineBreaks) 
  67.                     add-content -path $filename $ImageString 
  68.                     add-content -path $filename "`r`n"   
  69.                 } 
  70.         } 
  71.         catch{ 
  72.          
  73.         }    
  74.         add-content -path $filename "END:VCARD" 
  75.         "Exported " + $filename   
  76.     }  
  77.     catch{  
  78.     }  
  79. }