Hi there, i was hoping someone could help me integrate a powershell script i have into SAM?
Basically, this script sits on my Exchange 2010 server, and runs every 15 minutes. it checks where each DB is located, and if it finds one that is in the wrong place - it sends an email to the service desk. Just useful as there is no monitor in the Exchange 2010 monitors that duplicates this.
the script is as follows:
Function Get-ExchangeServerADSite ([String] $excServer)
{
# We could use WMI to check for the domain, but I think this method is better
# Get-WmiObject Win32_NTDomain -ComputerName $excServer
$configNC =([ADSI]"LDAP://RootDse").configurationNamingContext
$search = new-object DirectoryServices.DirectorySearcher([ADSI]"LDAP://$configNC")
$search.Filter = "(&(objectClass=msExchExchangeServer)(name=$excServer))"
$search.PageSize = 1000
[Void] $search.PropertiesToLoad.Add("msExchServerSite")
Try {
$adSite = [String] ($search.FindOne()).Properties.Item("msExchServerSite")
Return ($adSite.Split(",")[0]).Substring(3)
} Catch {
Return $null
}
}
[Bool] $bolFailover = $False
[String] $errMessage = $null
# Check if all databases are currently mounted on the server with ActivationPreference of 1
Get-MailboxDatabase | Where {$_.Recovery -eq $False} | Sort Name | ForEach {
$db = $_.Name
$curServer = $_.Server.Name
$ownServer = $_.ActivationPreference | ? {$_.Value -eq 1}
# Compare the server where the DB is currently active to the server where it should be
If ($curServer -ne $ownServer.Key.Name) {
# Get the AD sites of both servers
$siteCur = Get-ExchangeServerADSite $curServer
$siteOwn = Get-ExchangeServerADSite $ownServer.Key
# Check if both servers are on different AD sites
If ($siteCur -ne $null -and $siteOwn -ne $null -and $siteCur -ne $siteOwn) {
$errMessage += "`n$db on $curServer should be on $($ownServer.Key) (DIFFERENT AD SITE: $siteCur)!"
} Else {
$errMessage += "`n$db on $curServer should be on $($ownServer.Key)"
}
$bolFailover = $True
}
}
$errMessage += "`n`n"
# Check the Status of all databases including Content Index and Queues
Get-MailboxDatabase | Where {$_.Recovery -eq $False} | Sort Name | Get-MailboxDatabaseCopyStatus | ForEach {
If ($_.Status -notmatch "Mounted" -and $_.Status -notmatch "Healthy" -or $_.ContentIndexState -notmatch "Healthy" -or $_.CopyQueueLength -ge 200 -or $_.ReplayQueueLength -ge 200) {
$errMessage += "`n`n$($_.Name) - Status: $($_.Status) - Copy QL: $($_.CopyQueueLength) - Replay QL: $($_.ReplayQueueLength) - Index: $($_.ContentIndexState)"
$bolFailover = $True
}
}
If ($bolFailover) {
# Disable the schedule task that runs the script and send e-mail to administrator
Schtasks.exe /Change /TN "DAG Location" /DISABLE
Send-MailMessage -From "email.domain.com" -To "email.domain.com" -Cc "email.domain.com","email.domain.com","email.domain.com" -Subject "WARNING: Database Location Change" -Body $errMessage -Priority High -SMTPserver "server1" -DeliveryNotificationOption onFailure
}
I have tried looking at just a powershell monitor, but I don't know what argument I need to have, nor where to choose what makes it up or down. SAM is pretty new to me!
Thanks