Guest User

Untitled

a guest
Apr 24th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. SC failure w3svc reset= 432000 actions= restart/30000/restart/60000/run/60000
  2. SC failure w3svc command= "MyBatchFile.cmd"
  3.  
  4. blat - -body "Service W3svc Failed" -subject "SERVICE ERROR" -to Notify@Example.com -server SMTP.Example.com -f Administrator@Example.com
  5.  
  6. cd $PSScriptRoot
  7.  
  8. $n = $args[0]
  9.  
  10. function CreateLogFile {
  11. $events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
  12. if (!(Test-Path "c:temp")) {
  13. New-Item -Path "c:temp" -Type directory}
  14. if (!(Test-Path "c:tempServicesLogs.txt")) {
  15. New-Item -Path "c:temp" -Type File -Name "ServicesLogs.txt"}
  16. $events | Out-File -width 600 c:tempServicesLogs.txt
  17. }
  18.  
  19. function SendEmail {
  20. $EmailServer = "SMTP Server"
  21. $ToAddress = "Name@domain.com"
  22. $FromAddress = "Name@domain.com"
  23.  
  24. CreateLogFile
  25.  
  26. $Retrycount = $Retrycount + 1
  27. send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
  28. -Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:tempServicesLogs.txt
  29.  
  30. Remove-Item "c:tempServicesLogs.txt"
  31. }
  32.  
  33. function SendEmailFail {
  34. $EmailServer = "SMTP Server"
  35. $ToAddress = "Name@domain.com"
  36. $FromAddress = "Name@domain.com"
  37.  
  38. CreateLogFile
  39.  
  40. $Retrycount = $Retrycount + 1
  41. send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
  42. -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
  43.  
  44. Remove-Item "c:tempServicesLogs.txt"
  45. }
  46.  
  47. function StartService {
  48.  
  49. $Stoploop = $false
  50.  
  51. do {
  52. if ($Retrycount -gt 3){
  53. $Stoploop = $true
  54. SendEmail
  55. Break
  56. }
  57.  
  58. $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
  59. if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {
  60.  
  61. sc.exe start $n
  62. Start-Sleep -Seconds 35
  63.  
  64. $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
  65. if ($i.state -eq "Running"){
  66. $Stoploop = $true
  67. SendEmailFail}
  68. else {$Retrycount = $Retrycount + 1}
  69. }
  70. }
  71. While ($Stoploop -eq $false)
  72. }
  73.  
  74. [int]$Retrycount = "0"
  75. StartService
  76.  
  77. cd $PSScriptRoot
  78.  
  79. $n = $args[0]
  80.  
  81. function CreateLogFile {
  82. $events = Get-EventLog -LogName Application -Source SomeSource -Newest 40
  83. if (!(Test-Path "c:temp")) {
  84. New-Item -Path "c:temp" -Type directory}
  85. if (!(Test-Path "c:tempServicesLogs.txt")) {
  86. New-Item -Path "c:temp" -Type File -Name "ServicesLogs.txt"}
  87. $events | Out-File -width 600 c:tempServicesLogs.txt
  88. }
  89.  
  90. function SendEmail {
  91. $EmailServer = "SMTP Server"
  92. $ToAddress = "Name@domain.com"
  93. $FromAddress = "Name@domain.com"
  94.  
  95. CreateLogFile
  96.  
  97. $Retrycount = $Retrycount + 1
  98. send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" `
  99. -Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:tempServicesLogs.txt
  100.  
  101. Remove-Item "c:tempServicesLogs.txt"
  102. }
  103.  
  104. function SendEmailFail {
  105. $EmailServer = "SMTP Server"
  106. $ToAddress = "Name@domain.com"
  107. $FromAddress = "Name@domain.com"
  108.  
  109. CreateLogFile
  110.  
  111. $Retrycount = $Retrycount + 1
  112. send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" `
  113. -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
  114.  
  115. Remove-Item "c:tempServicesLogs.txt"
  116. }
  117.  
  118. function StartService {
  119.  
  120. $Stoploop = $false
  121.  
  122. do {
  123. if ($Retrycount -gt 3){
  124. $Stoploop = $true
  125. SendEmail
  126. Break
  127. }
  128.  
  129. $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode
  130. if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") {
  131.  
  132. sc.exe start $n
  133. Start-Sleep -Seconds 35
  134.  
  135. $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State
  136. if ($i.state -eq "Running"){
  137. $Stoploop = $true
  138. SendEmailFail}
  139. else {$Retrycount = $Retrycount + 1}
  140. }
  141. }
  142. While ($Stoploop -eq $false)
  143. }
  144.  
  145. [int]$Retrycount = "0"
  146. StartService
Add Comment
Please, Sign In to add comment