Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <#
  2.         ESM.ps1 - Exchange Service Monitor                                      
  3.         This script will connect to all the Exchange servers in the organization
  4.         It Will then Test Critical services based on server roles                
  5.         Services that are not running will be restarted                          
  6.         Written by David Sankovsky, Version 1.0 August 29th, 2017                
  7.     #>
  8.      
  9.     #Set Up Critical Services based on server roles
  10.     $MBXservices="IISAdmin","MSExchangeADTopology","MSExchangeDelivery","MSExchangeIS","MSExchangeMailboxAssistants","MSExchangeRepl","MSExchangeRPC","MSExchangeServiceHost","MSExchangeSubmission","MSExchangeThrottling","MSExchangeTransportLogSearch","W3Svc","WinRM" #MBX Services
  11.     $CASServices="Spooler","MSExchangeIMAP4","MSExchangeMailboxReplication","MSExchangeEdgeSync","MSExchangeTransport" #CAS Specific Services
  12.     $CAS7Services="MSExchangeADTopology","MSExchangeDiagnostics","MSExchangeFrontEndTransport","MSExchangeHM","MSExchangeServiceHost","MSExchangeUMCR"
  13.      
  14.     $LOGFILE="C:\Monitoring\log.txt" #Log File location
  15.      
  16.     <#
  17.         The Telegram function receives a service name and a server as parameters,
  18.         if invoked, the function will send a message through the telegram APP using a specific bot opened by HNF.
  19.         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.
  20.     #>
  21.      
  22.     function Telegram {
  23.         Param ([String]$Service,[String]$Server)
  24.         $URI = "https://api.telegram.org/bot###:***/sendMessage"
  25.         $targets = "###","###","###","###"
  26.         $text = "$service could not be restarted on $server! Critical service is down!"
  27.         Foreach ($target in $targets){
  28.             $querystring = $URI+"?chat_id=$target&text="+$text.Replace(' ','+')
  29.             WriteLog "Sending $text to TelegramID $target"
  30.             Invoke-WebRequest -Uri $querystring
  31.         }
  32.     }
  33.      
  34.     <#
  35.         WriteLog Function Writes log messages to the Log File and appends the TimeStamp before the actual log entry.
  36.         The Time Stamp is re-evaluated every time the function is called to allow for a more granular log search
  37.         Time Stamp format is [DD\MM\YYYY HH: mm]
  38.     #>
  39.     function WriteLog {
  40.         Param ([String] $Message)
  41.         $tStamp=Get-Date
  42.         $Day=$($tStamp).Day
  43.         $Month=$($tStamp).Month
  44.         $Year=$($tStamp).Year
  45.         $Hour=$($tStamp).Hour
  46.         $Minute=$($tStamp).Minute
  47.      
  48.         echo "[$Day\$Month\$Year $Hour`:$Minute] $Message" >> $LOGFILE
  49.     }
  50.      
  51.     <#
  52.         SendMail Function receives a service name, a Server name and a status
  53.         The function then sends a mail to sticket based on the received status
  54.             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
  55.             if the status is "restarted": The system will inform that the service was successfully restarted and that no further action is required.
  56.             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
  57.     #>
  58.     function SendMail {
  59.         Param ([String]$Service,[String]$Server,[String]$Status)
  60.         echo "$Service on $Server"
  61.         if ($Status.Equals("Failed")) {
  62.             #echo "$Service failed on $Server"
  63.             Send-MailMessage -From "ExchangeMonitor@domain.com" -To "sticket@domain.com" -Subject "Detected Service Failure on $Server! $Service is down!" -SmtpServer "server"
  64.         }
  65.         elseif ($Status.Equals("Restarted")){
  66.             Send-MailMessage -From "ExchangeMonitor@domain.com" -To "sticket@domain.com" -Subject "The $Service Service was succesfully restarted on $Server!" -SmtpServer "server"
  67.         }
  68.         else{
  69.             Send-MailMessage -From "ExchangeMonitor@domain.com" -To "sticket@domain.com" -Subject "Could not restart $Service on $Server!  Critical Service is down!" -SmtpServer "server"
  70.             Telegram $Service $Server
  71.         }
  72.     }
  73.      
  74.     #Add PowerShell Exchange commands Snap-in (Depending on which server is running this script, this command could be omitted
  75.     Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Snapin
  76.      
  77.     do{ #monitoring script logic starts here
  78.         $servers=$(Get-ExchangeServer)#Get all available Exchange servers
  79.         Foreach ($server in $servers){ #Run through one server at a time
  80.             $FailedService=0 #Count the number of Failed services per serer
  81.             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
  82.                     ForEach ($CASService in $CASServices){ #Run through all the services listed in CASServices
  83.                     $ServiceStatus=Get-Service -Name $CASService -ComputerName $($server.Name) #Remotely ascertain the status of the service in question.
  84.                     if ($ServiceStatus.Status -ne "Running"){ #If the service is not running as it should
  85.                         WriteLog "The Service $((Get-Service -Name $CASService).DisplayName.ToString()) is down on $server"
  86.                         SendMail $((Get-Service -Name $CASService).DisplayName.ToString()) ($($server.Name)).ToString() "Failed"
  87.                         Start-Service -Name $CASService #Attempt to restart the service.
  88.                         Start-Sleep -Seconds 120 #wait for 2 minutes
  89.                         $ServiceStatus=Get-Service -Name $CASService -ComputerName $($server.Name) #retest said service
  90.                         if ($ServiceStatus.Status -ne "Running"){
  91.                             $FailedService+=1 # if service did not start properly after 2 minutes, Increase the failed service counter
  92.                             WriteLog "Coiuld not recover $((Get-Service -Name $CASService).DisplayName.ToString()) on $server!"
  93.                             SendMail $((Get-Service -Name $CASService).DisplayName.ToString()) ($($server.Name)).ToString() "ERR"
  94.                         }
  95.                         else {
  96.                             WriteLog "The Service $((Get-Service -Name $CASService).DisplayName.ToString()) recovered on $server!"
  97.                             SendMail $((Get-Service -Name $CASService).DisplayName.ToString()) ($($server.Name)).ToString() "Restarted"
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.             if(-not ($($server.name.ToString().Contains("CAS7")))){
  103.                 ForEach ($MBXService in $MBXServices){
  104.                     $ServiceStatus=Get-Service -Name $MBXService -ComputerName $($server.Name)
  105.                     if ($ServiceStatus.Status -ne "Running"){
  106.                         sendMail $((Get-Service -Name $MBXService).DisplayName.ToString()) ($($server.Name)).ToString() "Failed"
  107.                         Start-Service -Name $MBXService #Attempt to restart the service
  108.                         Start-Sleep -Seconds 120 #Wait for 2 minutes
  109.                         $ServiceStatus=Get-Service -Name $MBXService -ComputerName $($server.Name) #retest the service
  110.                         if ($ServiceStatus.Status -ne "Running"){
  111.                             $FailedService+=1
  112.                             SendMail $((Get-Service -Name $MBXService).DisplayName.ToString()) ($($server.Name)).ToString() "ERR"
  113.                         }
  114.                         else{
  115.                             SendMail $((Get-Service -Name $MBXService).DisplayName.ToString()) ($($server.Name)).ToString() "Restarted"
  116.                         }
  117.                     }
  118.                 }
  119.             }
  120.             if ($($server.name.ToString().Contains("Cas7"))){
  121.                 ForEach ($CAS7Service in $CAS7Services){ #Run through all the services listed in CASServices
  122.                     $ServiceStatus=Get-Service -Name $CAS7Service -ComputerName $($server.Name) #Remotely ascertain the status of the service in question.
  123.                     if ($ServiceStatus.Status -ne "Running"){ #If the service is not running as it should
  124.                         WriteLog "The Service $((Get-Service -Name $CAS7Service).DisplayName.ToString()) is down on $server"
  125.                         SendMail $((Get-Service -Name $CAS7Service).DisplayName.ToString()) ($($server.Name)).ToString() "Failed"
  126.                         Start-Service -Name $CAS7Service #Attempt to restart the service.
  127.                         Start-Sleep -Seconds 120 #wait for 2 minutes
  128.                         $ServiceStatus=Get-Service -Name $CAS7Service -ComputerName $($server.Name) #retest said service
  129.                         if ($ServiceStatus.Status -ne "Running"){
  130.                             $FailedService+=1 # if service did not start properly after 2 minutes, Increase the failed service counter
  131.                             WriteLog "Coiuld not recover $((Get-Service -Name $CAS7Service).DisplayName.ToString()) on $server!"
  132.                             SendMail $((Get-Service -Name $CAS7Service).DisplayName.ToString()) ($($server.Name)).ToString() "ERR"
  133.                         }
  134.                         else {
  135.                             WriteLog "The Service $((Get-Service -Name $CAS7Service).DisplayName.ToString()) recovered on $server!"
  136.                             SendMail $((Get-Service -Name $CAS7Service).DisplayName.ToString()) ($($server.Name)).ToString() "Restarted"
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.         WriteLog "Finished a full run, Waiting 10 minutes"
  143.         Start-Sleep -Seconds 600 #Run script every 10 minutes
  144.     }until($infinity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement