by

Deleted Objects Folder merchandise kind retention statistics script

Deleted Objects Folder merchandise kind retention statistics script It is a followup publish to final weeks Mailbox Merchandise kind script primarily based on a query any individual requested in regards to the Deleted Objects folder and retention of various merchandise sorts. So what this script does is produces a extra detailed ItemType report for all of the Objects on this case simply in a single specific folder the Deleted Objects Folder. As a substitute of utilizing a Search it simply enumerates although all of the Objects within the goal folder and makes use of some logic to calculate just a few completely different statistics for Objects in that folder. Here is the kind of report it produces.

So the script experiences  on

  • Whole variety of Objects for every ItemType
  • Whole Measurement of Objects per ItemType
  • Oldest and Latest Acquired Date for every ItemType
  • Utilizing the PR_RetentionDate prolonged property (if its set) it really works out the variety of days left till the Merchandise expires after which calculates the variety of Objects that can expire inside 7, 7-14,14-30 and over 30 days (or in no way) for every ItemType.

 To run the script you simply enter the Mailbox you wish to run towards and the total path the Output csv file eg

 Get-DeletedItemsStats -MailboxName mailbox@area.com -ReportFileName c:temprep.csv

I’ve additionally created one other model of the script that takes a -FolderPath parameter so you possibly can run it towards any folder within the mailbox

eg to run it towards the muddle folder

 Get-FolderItemsStats -MailboxName mailbox@area.com -FolderPath ‘Muddle’ -ReportFileName c:temprep.csv

or the Junk Mail folder

 Get-FolderItemsStats -MailboxName mailbox@area.com -FolderPath ‘Junk Electronic mail’ -ReportFileName c:temprep.csv

I’ve put a download of each scripts right here

The code itself seems like

####################### 
<#
.SYNOPSIS
Creates a Statistics Report of Objects within the Deleted Objects folder in a Mailbox utilizing the Alternate Internet Companies API

.DESCRIPTION
Creates a Statistics Report of Objects within the Deleted Merchandise folder in a Mailbox utilizing the Alternate Internet Companies API

Requires the EWS Managed API from https://www.microsoft.com/en-us/download/particulars.aspx?id=42951

.EXAMPLE
PS C:>Get-DeletedItemsStats -MailboxName consumer.title@area.com -ReportFileName c:tempdelItemStats.csv
This Instance creates a Creates a Statistics Report of Objects within the Deleted Objects folder in a Mailbox
#>
perform Get-DeletedItemsStats
{
[CmdletBinding()]
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$true)] [string]$ReportFileName
)
Start
{
## Load Managed API dll
###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
$EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINESOFTWAREMicrosoftExchangeWeb Companies'|Kind-Object Title -Descending| Choose-Object -First 1 -ExpandProperty Title)).'Set up Listing') + "Microsoft.Exchange.WebServices.dll")
if (Take a look at-Path $EWSDLL)
{
Import-Module $EWSDLL
}
else
{
"$(get-date -format yyyyMMddHHmmss):"
"This script requires the EWS Managed API 1.2 or later."
"Please download and install the current version of the EWS Managed API from"
"http://go.microsoft.com/fwlink/?LinkId=255472"
""
"Exiting Script."
exit
}

## Set Alternate Model
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Alternate2010_SP2

## Create Alternate Service Object
$service = New-Object Microsoft.Alternate.WebServices.Information.ExchangeService($ExchangeVersion)

## Set Credentials to make use of two choices are availible Option1 to make use of explict credentials or Choice 2 use the Default (logged On) credentials

#Credentials Choice 1 utilizing UPN for the home windows Account
#$psCred = Get-Credential
$creds = New-Object System.Internet.NetworkCredential($Credentials.UserName.ToString(),$Credentials.GetNetworkCredential().password.ToString())
$service.Credentials = $creds
#Credentials Choice 2
#service.UseDefaultCredentials = $true
#$service.TraceEnabled = $true
## Select to disregard any SSL Warning points brought on by Self Signed Certificates

## Code From http://poshcode.org/624
## Create a compilation atmosphere
$Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Supplier.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
namespace Native.ToolkitExtensions.Internet.CertificatePolicy{
public class TrustAll : System.Internet.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Internet.ServicePoint sp,
System.Safety.Cryptography.X509Certificates.X509Certificates cert,
System.Internet.WebRequest req, int downside) {
return true;
}
}
}
'@
$TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an occasion of the TrustAll and fasten it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## finish code from http://poshcode.org/624

## Set the URL of the CAS (Shopper Entry Server) to make use of two choices are availbe to make use of Autodiscover to seek out the CAS URL or Hardcode the CAS to make use of

#CAS URL Choice 1 Autodiscover
$service.AutodiscoverUrl($MailboxName,{$true})
"Using CAS Server : " + $Service.url

#CAS URL Choice 2 Hardcoded

#$uri=[system.URI] "https://casservername/ews/exchange.asmx"
#$service.Url = $uri

## Elective part for Alternate Impersonation

#$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Information.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
# Bind to Deleted Objects Folder
$folderid= new-object Microsoft.Alternate.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::DeletedItems,$MailboxName)
$DeletedItems = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Outline ItemView to retrive simply 1000 Objects
$ivItemView = New-Object Microsoft.Alternate.WebServices.Information.ItemView(1000)
$PR_RETENTION_DATE = New-Object Microsoft.Alternate.WebServices.Information.ExtendedPropertyDefinition(0x301C,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime);
$ItemPropset= new-object Microsoft.Alternate.WebServices.Information.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$ItemPropset.Add($PR_RETENTION_DATE);
$ivItemView.PropertySet = $ItemPropset
$rptCollection = @{}
$fiItems = $null
do{
$fiItems = $service.FindItems($DeletedItems.Id,$ivItemView)
#[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
foreach($Merchandise in $fiItems.Objects){
#Course of Merchandise
if($rptCollection.Incorporates($Merchandise.ItemClass) -eq $false){
$rptObj = "" | Choose ItemClass,NumberOfItems,SizeOfItems,OldestItem,NewestItem,RetentionExpire7,RetentionExpire7To14,RetentionExpire14To30,RetentionExpire30Plus,NoRetention
$rptObj.OldestItem = $Merchandise.datetimereceived
$rptObj.NewestItem = $Merchandise.datetimereceived
$rptObj.ItemClass = $Merchandise.ItemClass
$rptObj.NumberOfItems = 1
$rptObj.SizeOfItems = $Merchandise.Measurement
$rptObj.RetentionExpire7 = 0
$rptObj.RetentionExpire7To14 = 0
$rptObj.RetentionExpire14To30 = 0
$rptObj.RetentionExpire30Plus = 0
$rptObj.NoRetention = 0
$RetvalObj = $null
if($Merchandise.TryGetProperty($PR_RETENTION_DATE,[ref]$RetvalObj)){
$rDays = ($RetvalObj - (Get-Date)).Days
if($rDays -le 7){
$rptObj.RetentionExpire7++
}
if($rDays -gt 7 -band $rDays -le 14 ){
$rptObj.RetentionExpire7To14++
}
if($rDays -gt 14 -band $rDays -le 30 ){
$rptObj.RetentionExpire14To30++
}
if($rDays -gt 30){
$rptObj.RetentionExpire30Plus++
}
}
else{
$rptObj.NoRetention++
}
$rptCollection.Add($Merchandise.ItemClass,$rptObj)
}
else{
if($Merchandise.datetimereceived -ne $null){
if($Merchandise.datetimereceived -gt $rptObj.NewestItem){
$rptObj.NewestItem = $Merchandise.datetimereceived
}
if($Merchandise.datetimereceived -lt $rptObj.OldestItem){
$rptObj.OldestItem = $Merchandise.datetimereceived
}
}
$RetvalObj = $null
if($Merchandise.TryGetProperty($PR_RETENTION_DATE,[ref]$RetvalObj)){
$rDays = ($RetvalObj - (Get-Date)).Days
if($rDays -le 7){
$rptCollection[$Merchandise.ItemClass].RetentionExpire7++
}
if($rDays -gt 7 -band $rDays -le 14 ){
$rptCollection[$Merchandise.ItemClass].RetentionExpire7To14++
}
if($rDays -gt 14 -band $rDays -le 30 ){
$rptCollection[$Merchandise.ItemClass].RetentionExpire14To30++
}
if($rDays -gt 30){
$rptCollection[$Merchandise.ItemClass].RetentionExpire30Plus++
}
}
else{
$rptCollection[$Merchandise.ItemClass].NoRetention++
}
$rptCollection[$Merchandise.ItemClass].NumberOfItems += 1
$rptCollection[$Merchandise.ItemClass].SizeOfItems += $Merchandise.Measurement
}
}
$ivItemView.Offset += $fiItems.Objects.Rely
}whereas($fiItems.MoreAvailable -eq $true)
$rptCollection.Values | Export-Csv -NoTypeInformation -Path $ReportFileName
Write-Output $rptCollection.Values
}
}