<#
.SYNOPSIS
check for mails in SCSM-mailboxes on exchange server
.DESCRIPTION
Use this script to check SCSM-mailboxes for stucked messages if processing of mails failed.
.PARAMETER $credential
optional, pass network credential with get-credential
.NOTES
Author: Andreas, Eric and Thomas, Data One GmbH
Date: 2015-02-28
LastEdit: 2015-02-28
Changelog:
2015-02-28
– initial version
.Link
Script: http://doitcloudy.blogspot.de/2015/02/powershell-uberwachung-von-scsm.html
#>
param ([System.Management.Automation.PSCredential] $credential) #pass credentials
#specify the delay in minutes
$threshold = (get-date).AddMinutes(-15)
[Reflection.Assembly]::LoadFrom(„C:Program FilesMicrosoft System Center 2012 R2Service ManagerMicrosoft.Exchange.WebServices.dll“)
Import-Module SMLets
#get the mailboxes from SCSM, special filter to exclude mailboxes with „SCSM“ in displayname
$connectors = Get-SCSMObject -Class (Get-SCSMClass -Name Microsoft.SystemCenter.Connector) | ?{$_.DataProviderDisplayName -eq „Exchange Connector“ -AND $_.DisplayName -notlike „*(SCSM)*“ -AND $_.Enabled} | select ImpersonationEmailAddress
$exService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
#use credential, which was passed to the script
if ($credential) {
$exService.Credentials = $credential.GetNetworkCredential()
}
#if no credential was passed to the script, use the credential from logged on user
else {
$exService.UseDefaultCredentials = $true
}
function GetInboxMails([string] $emailAddress) {
$exService.AutodiscoverUrl($emailAddress, {$true})
$mailbox = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($emailAddress)
$folderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $mailbox)
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exService,$folderid)
$itemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(250)
$propertySet= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
$propertySet.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Size)
$propertySet.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived)
$propertySet.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeCreated)
$itemView.PropertySet = $propertySet
$searchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,$threshold)
$mails = $exService.FindItems($inbox.Id,$searchFilter,$itemView)
If ($mails.TotalCount -ne „0“) {
$totalCount = $mails.TotalCount
$Address = $mailbox.address
#write event into the application log
$EventLog = New-Object System.Diagnostics.EventLog(„Application“)
$EventLog.Source = „SCSMMailCheck“
$EventLog.MachineName = „.“
$EventLog.WriteEntry(„There are $TotalCount e-mails older than 15 minutes in Inbox folder of mailbox $Address. Please review manually.“, „Error“, 75);
}
}
#loop through each mailbox and check for mails
Foreach ($Connector in $Connectors) {
GetInboxMails($Connector.ImpersonationEmailAddress)
}