############################################################################### # EWS Impersonation # EWS Managed API DEMO # Version 1.0 / 15.07.2012 # Andres Bohren / www.icewolf.ch / blog.icewolf.ch / info@icewolf.ch ############################################################################### ############################################################################### # Function WriteLog ############################################################################### Function WriteLog { PARAM ( [string]$pLogtext ) $pDate = $(get-date -format "dd.MM.yyyy HH:mm:ss") $sw = new-object system.IO.StreamWriter($LogPath, 1) $sw.writeline($pDate + " " + $pLogtext) $sw.close() } ############################################################################### # Function GetMailboxFolders ############################################################################### Function GetMailboxFolders { Try { #$pMBXFolders = new-Object system.collections.arraylist $pMBXFolders = @() $View = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000) $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly) $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName) $psPropertySet.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::ChildFolderCount) $View.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep $View.PropertySet = $psPropertySet $ExResult = $EWService.FindFolders([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot, $View) If ($ExResult -ne $Null) { Write-Host ("#Get Folders") WriteLog ("#Get Folders") ForEach ($myFolder In $ExResult.Folders) { #Write-Host ("Folder: " + $myFolder.DisplayName) #Write-Host ("Folder: " + $myFolder.Id) WriteLog ("Folder: " + $myFolder.DisplayName) Writelog ("Folder: " + $myFolder.Id) $pMBXFolders += $myFolder.DisplayName #$pMBXFolders += $myFolder.ID } } return ,$pMBXFolders } catch [system.exception] { WriteLog ("Error in GetMailboxFolders " + $_.Exception.ToString()) Write-Host ("Error in GetMailboxFolders " + $_.Exception.ToString()) } } ############################################################################### # MainProgramm ############################################################################### # Global Vars [string]$EwsApiDll = "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll" [string]$LogPath = "T:\Visual Basic\PowerShell\EWSScript\Impersonate.log" [string]$Email = "ewservice@icewolf.ch" [string]$Username = "ewservice" [string]$Password = "MySecredPassword!" [string]$Domain = "Corp" [string]$EWSURL = "" #[string]$EWSURL = "https://icesrv01/EWS/Exchange.asmx" #[string]$EWSURL = "https://icesrv01.corp.icewolf.ch/EWS/Exchange.asmx" [string]$ImpersonationMailbox = "boa@icewolf.ch" # Main Programm Try { WriteLog "###############################################################################" WriteLog "### Starting Script" WriteLog "###############################################################################" # Import EWS Managed API DLL Import-Module -Name $EwsApiDll Write-Host ("Imported EWS Module") WriteLog ("Imported EWS Module") # Connect to EWS #$EWService = ConnectEWS ($Email, $Username, $Password, $Domain, $EWSURL) # Create a new Exchange Service Object $EWService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) $EWService.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials($Username,$Password,$Domain) Write-Host ("Connecting to EWS...") WriteLog ("Connecting to EWS...") If ($EWSURL -eq "") { $EWService.AutodiscoverUrl($Email) Write-Host ("Using Autodiscover") } else { $EWService.Url = $EWSURL Write-Host ("Using EWS URL") } $Mailbox = $ImpersonationMailbox Write-Host ("-->Impersonation to Mailbox: " + $Mailbox) Writelog ("-->Impersonation to Mailbox: " + $Mailbox) $EWService.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $Mailbox) # Get Mailbox Folders $Folders = GetMailboxFolders Foreach ($Folder in $Folders) { Write-Host ("Folder: " + $Folder) WriteLog ("Folder: " + $Folder) } # Clean Up WriteLog "###############################################################################" WriteLog "### Finished" WriteLog "###############################################################################" Write-Host "Finished" } catch [system.exception] { WriteLog ("Error in EmptyRecycleBin " + $_.Exception.ToString()) Write-Host ("Error in EmptyRecycleBin " + $_.Exception.ToString()) }