none
EWS script to return email items retention tags - not returning any results RRS feed

  • Question

  • Hi there

    I am trying to write a function to return individual items (emails) in a folder - retention policy whether explicitly stamped or not - this function reports the folders policy tag and retentionaction/period - fine but does not work for the individual items

    I can see the $item has a policytag property but it is blank for some reason - and is not giving the correct tag name and retentionperiod - not sure why? Here is the function: Could someone share some light on this?  Users are a mix of EOL and exchange 2013 onprem - thankyou

    function Get-MailboxFoldertags($user)
      {

           
            $retentioninfo = @()
            $retentiontags = @()
            $pageSize =5
            $Offset = 0
            $emailinfo = @()
                


                            write-host -ForegroundColor DarkBlue "DEF in function"
                            $FPageSize = 500
                            $FOffset = 0
                           
                            $folderView = new-object Microsoft.Exchange.WebServices.Data.FolderView($FPageSize,$FOffset,[Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning)
                            $folderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
                            $oFindFolders = $exchangeService.FindFolders([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$null,$folderView) | 
                            ?{($_.DisplayName -eq 'George Thomas')}
                
                            $RetentionTags = $exchangeService.GetUserRetentionPolicyTags()
                    

                                function GetTagName($tagGUID) {
                                if (!$tagGUID) { return ($RetentionTags.RetentionPolicyTags | ? {$_.Type -eq "All"}).DisplayName }
                                foreach ($tag in $RetentionTags.RetentionPolicyTags) {
                                if ($tag.RetentionId -eq $tagGUID ) { return $tag.DisplayName }
                                }
                            }


                                function GetRetentionAction($tagGUID)  {
                                if (!$tagGUID) { return ($RetentionTags.RetentionPolicyTags | ? {$_.Type -eq "All"}).RetentionAction }
                                foreach ($tag in $RetentionTags.RetentionPolicyTags) {
                                if ($tag.RetentionId -eq $tagGUID ) { return $tag.RetentionAction }

                                }
                            }

                                 function GetRetentionPeriod($tagGUID)  {
                                if (!$tagGUID) { return ($RetentionTags.RetentionPolicyTags | ? {$_.Type -eq "All"}).RetentionPeriod }
                                foreach ($tag in $RetentionTags.RetentionPolicyTags) {
                                if ($tag.RetentionId -eq $tagGUID ) { return $tag.RetentionPeriod }

                                }
                            }
                                
                           
                           
                               #Setup the View to get a limited number of Items at one time
            $itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView($pageSize,$Offset,[Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning)
            $itemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Shallow
            $itemView.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet(
                         
                                
                                [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Subject)
                                 $oFindFolders | %{
                                    $oFinditems = $exchangeService.FindItems($_.Id,$itemview)
                                        
                                    $obj = New-Object PSObject
                                    $obj | add-member noteproperty DisplayName $_.DisplayName
                                    $obj | add-member noteproperty PolicyTag (GetTagName $_.PolicyTag.RetentionId)
                                    $obj | add-member noteproperty ArchiveTag (GetTagName $_.ArchiveTag.RetentionId)                        
                                    $obj | add-member noteproperty RetentionActionPolicyTag (GetRetentionAction $_.PolicyTag.RetentionId)
                                    $obj | add-member noteproperty RetentionActionArchiveTag (GetRetentionAction $_.ArchiveTag.RetentionId)
                                    $obj | add-member noteproperty RetentionPeriodPolicyTag (GetRetentionPeriod $_.PolicyTag.RetentionId)
                                    $obj | add-member noteproperty RetentionPeriodArchiveTag (GetRetentionPeriod $_.ArchiveTag.RetentionId)
                                    $obj | add-member noteproperty Usermailbox $user
                                    $obj | add-member noteproperty FolderItemCount $_.TotalCount
                                    
                                    foreach ($Item in $oFindItems.Items)
                                    {

                                          
                                          $emailobj = New-Object PSObject
                                          $emailsubject = $item.subject
                                          

                                          $emailtagname = (GetTagName $item.PolicyTag.RetentionId)
                                          $emailtagaction = (GetRetentionAction $item.PolicyTag.RetentionId)
                                          $emailtagretentionperiod = (GetRetentionPeriod $item.PolicyTag.RetentionId)
                                          
                                          $emailobj | add-member noteproperty EmailSubject $emailsubject
                                          $emailobj | add-member noteproperty EmailTagName $emailtagname
                                          $emailobj | add-member noteproperty EmailTagAction $emailtagaction
                                          $emailobj | add-member noteproperty EmailTagPeriod $emailtagretentionperiod
                                          $emailobj | add-member noteproperty EmailParentFolder $obj.displayname
                                          $emailinfo += $emailobj

                                          

                                    }
                                    #$obj | add-member noteproperty EmailSubject ($emailinfo -join "\n" | out-string).Trim()
                                    
                                    
                                    $retentioninfo += $obj
                                           
                                

                                }
                                                           
                
        $emailinfo | Export-csv .\emailreport.csv -NoTypeInformation
        $retentioninfo | Export-Csv .\report.csv -NoTypeInformation
     
        
     }   

    Friday, August 24, 2018 2:22 PM

All replies

  • The first thing would be to check the ServerVersion you specifying eg

    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)  

    if your setting that to 2010 then that property won't be returned because its only returned when the requested server version is 2013 or greater. Also if your enable tracing (or look at the responses with something like fiddler) do you see the policyTag XML element being returned ?

    Cheers
    Glen

     
    Monday, August 27, 2018 5:30 AM
  • Hi 

    I got it to work using this code i found online:

    $PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) 
        #PR_POLICY_TAG 0x3019
                 $PolicyTag = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x3019,[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_RETENTION_PERIOD 0x301A
            $RetentionPeriod = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x301A,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
    $PR_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3592, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);  
    $PropertySet.Add($PolicyTag)
    $PropertySet.Add($RetentionFlags)
    $PropertySet.Add($RetentionPeriod)
    $PropertySet.Add($PR_MESSAGE_SIZE_EXTENDED) 
                $itemView.PropertySet = $PropertySet

    This code confuses me because I do not fully understand what it is doing - what are the FirstClassProperties and do not understand the lines of code for ExtendedPropertyDefinition(0x3019,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]

    and 

    $PR_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3592, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);

    even though the code works now :) why are these required?

    Regards

    Tuesday, August 28, 2018 10:21 AM
  • You should only need that for older versions of Exchange from 2013 on you should be able to use the PolicyTag Strongly typed property. Its the same information at the end of the day though

    Cheers
    Glen

    Tuesday, August 28, 2018 11:17 PM
  • Hi Glen

    not sure what you mean by "you should be able to use the PolicyTag Strongly typeed property" what does that mean? We use ex2013 CU20 onprem

    Regards

    Wednesday, August 29, 2018 11:16 AM