Hi,
I'm configuring an retentiontag for archiving a specific folder after 2 days.
I'm performing this via EWS.
The goal is to stamp a specific folder (called "TESTFOLDER" that's available within every mailbox), with a personal tag I created.
So far I've modified the script I've found here :
http://blogs.msdn.com/b/akashb/archive/2012/12/07/stamping-archive-policy-tag-using-ews-managed-api-from-powershell-exchange-2010.aspx?CommentPosted=true#commentmessage
1) created a personal tag called "1_Day_move_to_Archive"
2) Changed some settings of the code (EWS version, paths, etc.)
3) configured my account with impersonation rights. (to my own mailbox)
4) Inserted the GUID of the created tag into the script
5) Changed this value "$oFolder.SetExtendedProperty($RetentionFlags, 16)" to "$oFolder.SetExtendedProperty($RetentionFlags, 8)"
16 specifies ExplicitArchiveTag, 8 specifies personaltag >> I tried both settings here
Now when I run the script I don't get any errors thrown, and I get the prompt that the policy is stamped.
When I check afterwards via outlook/owa, I don't see any change however.
I already forced the folder assistant to run, but it also doesn't change anything.
Anyone knows what might be wrong?
# The script requires the EWS managed API, which can be downloaded here:
#
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
# This also requires PowerShell 2.0
# Make sure the Import-Module command below matches the DLL location of the API.
# This path must match the install location of the EWS managed API. Change it if needed.
[string]$info = "White" # Color for informational messages
[string]$warning = "Yellow" # Color for warning messages
[string]$error = "Red" # Color for error messages
[string]$LogFile = "C:\Temp\Log.txt" # Path of the Log File
function StampPolicyOnFolder($MailboxName)
{
Write-host "Stamping Policy on folder for Mailbox Name:" $MailboxName -foregroundcolor $info
Add-Content $LogFile ("Stamping Policy on folder for Mailbox Name:" + $MailboxName)
#Change the user to Impersonate
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName);
#Search for the folder you want to stamp the property on
$oFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
$oSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$FolderName)
$oFindFolderResults = $service.FindFolders([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$oSearchFilter,$oFolderView)
if ($oFindFolderResults.TotalCount -eq 0)
{
Write-host "Folder does not exist in Mailbox:" $MailboxName -foregroundcolor $warning
Add-Content $LogFile ("Folder does not exist in Mailbox:" + $MailboxName)
}
else
{
Write-host "Folder found in Mailbox:" $MailboxName -foregroundcolor $info
#PR_ARCHIVE_TAG 0x3018 – We use the PR_ARCHIVE_TAG instead of the PR_POLICY_TAG
$ArchiveTag = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x3018,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);
#PR_RETENTION_FLAGS 0x301D
$RetentionFlags = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x301D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
#PR_ARCHIVE_PERIOD 0x301E - We use the PR_ARCHIVE_PERIOD instead of the PR_RETENTION_PERIOD
$ArchivePeriod = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x301E,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
#Change the GUID based on your policy tag
$ArchiveTagGUID = new-Object Guid("{7d71ddd7-061a-473e-8b92-033e126bbcb0}");
#Bind to the folder found
$oFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$oFindFolderResults.Folders[0].Id)
#Same as that on the policy - 16 specifies that this is a ExplictArchiveTag -- EDIT (also tried with value 8 > personal tag)
$oFolder.SetExtendedProperty($RetentionFlags, 16)
#Same as that on the policy - Since this tag is disabled the Period would be 0 -- We choose 1, since we want to archive after 1 day
$oFolder.SetExtendedProperty($ArchivePeriod, 2)
$oFolder.Update()
Write-host "Retention policy stamped!" -foregroundcolor $info
Add-Content $LogFile ("Retention policy stamped!")
}
$service.ImpersonatedUserId = $null
}
#Change the name of the folder. This is the folder the properties will be stamped on.
$FolderName = "TESTFOLDER"
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
# Set the Credentials
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("MyAccount","MyPassword","MyDomain")
# Change the URL to point to your cas server
$service.Url = new-object Uri("https://owa.customer.int/EWS/Exchange.asmx")
# Set $UseAutoDiscover to $true if you want to use AutoDiscover else it will use the URL set above
$UseAutoDiscover = $false
#Read data from the UserAccounts.txt.
#This file must exist in the same location as the script.
import-csv UserAccounts.txt | foreach-object {
$WindowsEmailAddress = $_.WindowsEmailAddress.ToString()
if ($UseAutoDiscover -eq $true) {
Write-host "Autodiscovering.." -foregroundcolor $info
$UseAutoDiscover = $false
$service.AutodiscoverUrl("$WindowsEmailAddress", {$true})
Write-host "Autodiscovering Done!" -foregroundcolor $info
Write-host "EWS URL set to :" $service.Url -foregroundcolor $info
}
#To catch the Exceptions generated
trap [System.Exception]
{
Write-host ("Error: " + $_.Exception.Message) -foregroundcolor $error;
Add-Content $LogFile ("Error: " + $_.Exception.Message);
continue;
}
StampPolicyOnFolder($WindowsEmailAddress)
}