Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SC failure w3svc reset= 432000 actions= restart/30000/restart/60000/run/60000
- SC failure w3svc command= "MyBatchFile.cmd"
- blat - -body "Service W3svc Failed" -subject "SERVICE ERROR" -to Notify@Example.com -server SMTP.Example.com -f Administrator@Example.com
- cd $PSScriptRoot
- $n = $args[0]
- function CreateLogFile {
- $events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
- if (!(Test-Path "c:temp")) {
- New-Item -Path "c:temp" -Type directory}
- if (!(Test-Path "c:tempServicesLogs.txt")) {
- New-Item -Path "c:temp" -Type File -Name "ServicesLogs.txt"}
- $events | Out-File -width 600 c:tempServicesLogs.txt
- }
- function SendEmail {
- $EmailServer = "SMTP Server"
- $ToAddress = "Name@domain.com"
- $FromAddress = "Name@domain.com"
- CreateLogFile
- $Retrycount = $Retrycount + 1
- send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
- -Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:tempServicesLogs.txt
- Remove-Item "c:tempServicesLogs.txt"
- }
- function SendEmailFail {
- $EmailServer = "SMTP Server"
- $ToAddress = "Name@domain.com"
- $FromAddress = "Name@domain.com"
- CreateLogFile
- $Retrycount = $Retrycount + 1
- send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
- -Body "The $n service on server $env:COMPUTERNAME stopped and was successfully restarted after $Retrycount attempts. The relevant system logs are attached." -Attachments c:tempServicesLogs.txt
- Remove-Item "c:tempServicesLogs.txt"
- }
- function StartService {
- $Stoploop = $false
- do {
- if ($Retrycount -gt 3){
- $Stoploop = $true
- SendEmail
- Break
- }
- $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
- if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {
- sc.exe start $n
- Start-Sleep -Seconds 35
- $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
- if ($i.state -eq "Running"){
- $Stoploop = $true
- SendEmailFail}
- else {$Retrycount = $Retrycount + 1}
- }
- }
- While ($Stoploop -eq $false)
- }
- [int]$Retrycount = "0"
- StartService
- cd $PSScriptRoot
- $n = $args[0]
- function CreateLogFile {
- $events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
- if (!(Test-Path "c:temp")) {
- New-Item -Path "c:temp" -Type directory}
- if (!(Test-Path "c:tempServicesLogs.txt")) {
- New-Item -Path "c:temp" -Type File -Name "ServicesLogs.txt"}
- $events | Out-File -width 600 c:tempServicesLogs.txt
- }
- function SendEmail {
- $EmailServer = "SMTP Server"
- $ToAddress = "Name@domain.com"
- $FromAddress = "Name@domain.com"
- CreateLogFile
- $Retrycount = $Retrycount + 1
- send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
- -Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:tempServicesLogs.txt
- Remove-Item "c:tempServicesLogs.txt"
- }
- function SendEmailFail {
- $EmailServer = "SMTP Server"
- $ToAddress = "Name@domain.com"
- $FromAddress = "Name@domain.com"
- CreateLogFile
- $Retrycount = $Retrycount + 1
- send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
- -Body "The $n service on server $env:COMPUTERNAME stopped and was successfully restarted after $Retrycount attempts. The relevant system logs are attached." -Attachments c:tempServicesLogs.txt
- Remove-Item "c:tempServicesLogs.txt"
- }
- function StartService {
- $Stoploop = $false
- do {
- if ($Retrycount -gt 3){
- $Stoploop = $true
- SendEmail
- Break
- }
- $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
- if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {
- sc.exe start $n
- Start-Sleep -Seconds 35
- $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
- if ($i.state -eq "Running"){
- $Stoploop = $true
- SendEmailFail}
- else {$Retrycount = $Retrycount + 1}
- }
- }
- While ($Stoploop -eq $false)
- }
- [int]$Retrycount = "0"
- StartService
Add Comment
Please, Sign In to add comment