Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Move mail from Inbox to subfolder with Exchange Web Services 2.0 and Powershell

$
0
0

Hi

I have created a script (With help from Google and some blogs :-)) that basically does the following:
1. Finds all mails with attachments
2. Saves the attachment
 2.1 If it's a .ZIP file, the file is extracted
3. Prints the attachments
4. Moves the file to a subfolder called Archive (This part doesn't work

The steps from 1 to 3 is working fine, but the part about moving the items to the subfolder it fails.

I have tested it with my own username and it's working as expected, but when I run the script in production with the service account I have created with impersonation rights it fails.
As far as I can see the problem is that the service account can't get the ID of the subfolder. I use the following code:

$FolderName = "Archive"
$oFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($EWSService)
$oFolder.DisplayName = $FolderName

#Define Folder View really only want to return one object
$fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)

#Define a Search folder that is going to do a search based on the DisplayName of the folder
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$FolderName)

#Do the Search
$findFolderResults = $EWSService.FindFolders($InboxID,$SfSearchFilter,$fvFolderView)

If ($findFolderResults.TotalCount -ne 0)
{	
	$DestID = $findFolderResults.ID
	$Email.Move($DestID) | Out-Null
}

The "$findFolderResults.TotalCount" actually returns 1 if the folder is present but "$findFolderResults.ID" is empty, and therefor the "$Email.Move($DestID)" fails.

Any ideas?

Lasse


/Lasse


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>