Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- ESM.ps1 - Exchange Service Monitor
- This script will connect to all the Exchange servers in the organization
- It Will then Test Critical services based on server roles
- Services that are not running will be restarted
- Written by David Sankovsky, Version 1.0 August 29th, 2017
- #>
- #Set Up Critical Services based on server roles
- $MBXservices="IISAdmin","MSExchangeADTopology","MSExchangeDelivery","MSExchangeIS","MSExchangeMailboxAssistants","MSExchangeRepl","MSExchangeRPC","MSExchangeServiceHost","MSExchangeSubmission","MSExchangeThrottling","MSExchangeTransportLogSearch","W3Svc","WinRM" #MBX Services
- $CASServices="Spooler","MSExchangeIMAP4","MSExchangeMailboxReplication","MSExchangeEdgeSync","MSExchangeTransport" #CAS Specific Services
- $CAS7Services="MSExchangeADTopology","MSExchangeDiagnostics","MSExchangeFrontEndTransport","MSExchangeHM","MSExchangeServiceHost","MSExchangeUMCR"
- $LOGFILE="C:\Monitoring\log.txt" #Log File location
- <#
- The Telegram function receives a service name and a server as parameters,
- if invoked, the function will send a message through the telegram APP using a specific bot opened by HNF.
- All the recipients are configured in the "targets" array the script runs on each target separately so that if one causes an error, it won't break the script.
- #>
- function Telegram {
- Param ([String]$Service,[String]$Server)
- $URI = "https://api.telegram.org/bot###:***/sendMessage"
- $targets = "###","###","###","###"
- $text = "$service could not be restarted on $server! Critical service is down!"
- Foreach ($target in $targets){
- $querystring = $URI+"?chat_id=$target&text="+$text.Replace(' ','+')
- WriteLog "Sending $text to TelegramID $target"
- Invoke-WebRequest -Uri $querystring
- }
- }
- <#
- WriteLog Function Writes log messages to the Log File and appends the TimeStamp before the actual log entry.
- The Time Stamp is re-evaluated every time the function is called to allow for a more granular log search
- Time Stamp format is [DD\MM\YYYY HH: mm]
- #>
- function WriteLog {
- Param ([String] $Message)
- $tStamp=Get-Date
- $Day=$($tStamp).Day
- $Month=$($tStamp).Month
- $Year=$($tStamp).Year
- $Hour=$($tStamp).Hour
- $Minute=$($tStamp).Minute
- echo "[$Day\$Month\$Year $Hour`:$Minute] $Message" >> $LOGFILE
- }
- <#
- SendMail Function receives a service name, a Server name and a status
- The function then sends a mail to sticket based on the received status
- if the status is "failed": The system will inform that the service is down and that the system will attempt to restart the service on its own
- if the status is "restarted": The system will inform that the service was successfully restarted and that no further action is required.
- if the status is neither of the above (default is ERR): The system will inform that it was unable to automatically restart the service and that a sysadmins intervention is required, the system will also attempt to send an SMS
- #>
- function SendMail {
- Param ([String]$Service,[String]$Server,[String]$Status)
- echo "$Service on $Server"
- if ($Status.Equals("Failed")) {
- #echo "$Service failed on $Server"
- Send-MailMessage -From "ExchangeMonitor@domain.com" -To "sticket@domain.com" -Subject "Detected Service Failure on $Server! $Service is down!" -SmtpServer "server"
- }
- elseif ($Status.Equals("Restarted")){
- Send-MailMessage -From "ExchangeMonitor@domain.com" -To "sticket@domain.com" -Subject "The $Service Service was succesfully restarted on $Server!" -SmtpServer "server"
- }
- else{
- Send-MailMessage -From "ExchangeMonitor@domain.com" -To "sticket@domain.com" -Subject "Could not restart $Service on $Server! Critical Service is down!" -SmtpServer "server"
- Telegram $Service $Server
- }
- }
- #Add PowerShell Exchange commands Snap-in (Depending on which server is running this script, this command could be omitted
- Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Snapin
- do{ #monitoring script logic starts here
- $servers=$(Get-ExchangeServer)#Get all available Exchange servers
- Foreach ($server in $servers){ #Run through one server at a time
- $FailedService=0 #Count the number of Failed services per serer
- if ($($server.ServerRole.ToString()).Contains("Client") -and -not ($($server.name.ToString().Contains("CAS7")))){ #Check the server Roles, if contains the string "Client", the server is CAS
- ForEach ($CASService in $CASServices){ #Run through all the services listed in CASServices
- $ServiceStatus=Get-Service -Name $CASService -ComputerName $($server.Name) #Remotely ascertain the status of the service in question.
- if ($ServiceStatus.Status -ne "Running"){ #If the service is not running as it should
- WriteLog "The Service $((Get-Service -Name $CASService).DisplayName.ToString()) is down on $server"
- SendMail $((Get-Service -Name $CASService).DisplayName.ToString()) ($($server.Name)).ToString() "Failed"
- Start-Service -Name $CASService #Attempt to restart the service.
- Start-Sleep -Seconds 120 #wait for 2 minutes
- $ServiceStatus=Get-Service -Name $CASService -ComputerName $($server.Name) #retest said service
- if ($ServiceStatus.Status -ne "Running"){
- $FailedService+=1 # if service did not start properly after 2 minutes, Increase the failed service counter
- WriteLog "Coiuld not recover $((Get-Service -Name $CASService).DisplayName.ToString()) on $server!"
- SendMail $((Get-Service -Name $CASService).DisplayName.ToString()) ($($server.Name)).ToString() "ERR"
- }
- else {
- WriteLog "The Service $((Get-Service -Name $CASService).DisplayName.ToString()) recovered on $server!"
- SendMail $((Get-Service -Name $CASService).DisplayName.ToString()) ($($server.Name)).ToString() "Restarted"
- }
- }
- }
- }
- if(-not ($($server.name.ToString().Contains("CAS7")))){
- ForEach ($MBXService in $MBXServices){
- $ServiceStatus=Get-Service -Name $MBXService -ComputerName $($server.Name)
- if ($ServiceStatus.Status -ne "Running"){
- sendMail $((Get-Service -Name $MBXService).DisplayName.ToString()) ($($server.Name)).ToString() "Failed"
- Start-Service -Name $MBXService #Attempt to restart the service
- Start-Sleep -Seconds 120 #Wait for 2 minutes
- $ServiceStatus=Get-Service -Name $MBXService -ComputerName $($server.Name) #retest the service
- if ($ServiceStatus.Status -ne "Running"){
- $FailedService+=1
- SendMail $((Get-Service -Name $MBXService).DisplayName.ToString()) ($($server.Name)).ToString() "ERR"
- }
- else{
- SendMail $((Get-Service -Name $MBXService).DisplayName.ToString()) ($($server.Name)).ToString() "Restarted"
- }
- }
- }
- }
- if ($($server.name.ToString().Contains("Cas7"))){
- ForEach ($CAS7Service in $CAS7Services){ #Run through all the services listed in CASServices
- $ServiceStatus=Get-Service -Name $CAS7Service -ComputerName $($server.Name) #Remotely ascertain the status of the service in question.
- if ($ServiceStatus.Status -ne "Running"){ #If the service is not running as it should
- WriteLog "The Service $((Get-Service -Name $CAS7Service).DisplayName.ToString()) is down on $server"
- SendMail $((Get-Service -Name $CAS7Service).DisplayName.ToString()) ($($server.Name)).ToString() "Failed"
- Start-Service -Name $CAS7Service #Attempt to restart the service.
- Start-Sleep -Seconds 120 #wait for 2 minutes
- $ServiceStatus=Get-Service -Name $CAS7Service -ComputerName $($server.Name) #retest said service
- if ($ServiceStatus.Status -ne "Running"){
- $FailedService+=1 # if service did not start properly after 2 minutes, Increase the failed service counter
- WriteLog "Coiuld not recover $((Get-Service -Name $CAS7Service).DisplayName.ToString()) on $server!"
- SendMail $((Get-Service -Name $CAS7Service).DisplayName.ToString()) ($($server.Name)).ToString() "ERR"
- }
- else {
- WriteLog "The Service $((Get-Service -Name $CAS7Service).DisplayName.ToString()) recovered on $server!"
- SendMail $((Get-Service -Name $CAS7Service).DisplayName.ToString()) ($($server.Name)).ToString() "Restarted"
- }
- }
- }
- }
- }
- WriteLog "Finished a full run, Waiting 10 minutes"
- Start-Sleep -Seconds 600 #Run script every 10 minutes
- }until($infinity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement