EWS Exchange 2010 sp3 - script to export IPM.contacts from known public folder.

Hello,

We are using a powershell script on our EX2K10_SP3 server to export all contacts stored in all user mailboxes to a csv file. The script was found in several snippets through searching the internet as I am not a coder :-(Belwo is the relevant part of this script:

## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
        
$uri=[system.URI] "http://ex2k10_SP3/ews/exchange.asmx"    
$service.Url = $uri     

$outputfile = "E:\E2K10_SCRIPT\output\Kontakte_MBX_sample.csv"

#Header rows 
#'"Firma";"Vorname";"Nachname";"Strasse";"PLZ";"Stadt";"Land";' | Out-File -Encoding default -FilePath $outputfile -append

#traverse mailbox and pull out All Contacts
function getcontacts ([string]$MailboxName)
{
 $ContactsFolderid  = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$MailboxName) 
 $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000,0) 
 $findResults = $Service.FindItems($ContactsFolderid,$view)

foreach($item in $findResults)
 {
$contactdata = ($item.PhysicalAddresses[0].Street) -replace "`r|`n"," "
"$($item.CompanyName);$($item.GivenName);$($item.Surname);$contactdata;$($item.PhysicalAddresses[0].PostalCode);$($item.PhysicalAddresses[0].City);$($item.PhysicalAddresses[0].CountryOrRegion);" | Out-File -Encoding default -FilePath $outputfile -Append }
 }
#Pulls names from a csv file that you provide

foreach
($MailboxName in Get-Content "E:\E2K10_SCRIPT\Aliases.txt")
 { 
 getcontacts -MailboxName ($MailboxName)
 }

I am not a coder but I understand very limited the content of this script. Now I must do this against one known public Folder like \Contacts supplier . First of all I have now idea how to run this script against the public folder?

Any help and advise is appreciated. (The script should run once a week against one public folder, but it is not a WellKnownFolderName, so how access the folder?)

Regards,

Andreas


August 3rd, 2013 10:53am

To Access a Public folder with EWS you need to know its EWSId, there is no direct way of getting this so you need to search for it starting at the root of the Public Store eg something like this

function FolderIdFromPath{
	param (
	        $FolderPath = "$( throw 'Folder Path is a mandatory Parameter' )"
		  )
	process{
		## Find and Bind to Folder based on Path  
		#Define the path to search should be seperated with \  
		#Bind to the MSGFolder Root  
		$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)   
		$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
		#Split the Search path into an array  
		$fldArray = $FolderPath.Split("\") 
		 #Loop through the Split Array and do a Search for each level of folder 
		for ($lint = 1; $lint -lt $fldArray.Length; $lint++) { 
	        #Perform search based on the displayname of each folder level 
	        $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1) 
	        $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint]) 
	        $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView) 
	        if ($findFolderResults.TotalCount -gt 0){ 
	            foreach($folder in $findFolderResults.Folders){ 
	                $tfTargetFolder = $folder                
	            } 
	        } 
	        else{ 
	            "Error Folder Not Found"  
	            $tfTargetFolder = $null  
	            break  
	        }     
	    }  
		if($tfTargetFolder -ne $null){
			return $tfTargetFolder.Id.UniqueId.ToString()
		}
	}
}

#Example use
$fldId = FolderIdFromPath -FolderPath "\firstlevel\secondlevel\target"
$SubFolderId =  new-object Microsoft.Exchange.WebServices.Data.FolderId($fldId)
$PubFolder  = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$SubFolderId)
$PubFolder

One other suggestion is your code to get the Contacts won't get any more the 1000 contacts, so if you have 1001 contacts in a folder you will be missing one contact. Make sure you page through all the items in a folder using something like

 #Define ItemView to retrive just 1000 Items    
$ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)    
$fiItems = $null    
do{    
    $fiItems = $service.FindItems($PubFolder.Id,$ivItemView)    
    #[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)  
    foreach($Item in $fiItems.Items){      
				#Process Item          
    }    
    $ivItemView.Offset += $fiItems.Items.Count    
}while($fiItems.MoreAvailable -eq $true) 
Cheers
Glen
Free Windows Admin Tool Kit Click here and download it now
August 4th, 2013 3:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics