Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. #############################################################################
  2. # Author: Mahesh Sharma
  3. # Reviewer: Vikas SUkhija
  4. # Date: 06/10/2013
  5. # Modified:06/23/2013 - made it to run from any Location
  6. # Updated: 10/01/2017 - for alerting
  7. # Description: Services Monitor
  8. #############################################################################
  9. $alert = "Yes"
  10. $report = ".\report.htm"
  11.  
  12. ##############Funstions#########################
  13. function Send-Slack ($sename, $machinename, $status)
  14. {
  15.  
  16. Set-StrictMode -Version Latest
  17.  
  18. $payload = @{
  19. "channel" = "#enigmabox-t-clientes"
  20. "icon_emoji" = ":computer:"
  21. "text" = "Service $sename $machinename $status"
  22. "username" = "Demo"
  23. }
  24.  
  25. function ConvertTo([object] $item){
  26. add-type -assembly system.web.extensions
  27. $ps_js=new-object system.web.script.serialization.javascriptSerializer
  28. return $ps_js.Serialize($item)
  29. }
  30. $json = ConvertTo($payload)
  31.  
  32. Write-Host $json
  33. $url = "https://hooks.slack.com/services/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"
  34. $postData = $json
  35.  
  36. $buffer = [text.encoding]::ascii.getbytes($postData)
  37.  
  38. [net.httpWebRequest] $req = [net.webRequest]::create($url)
  39. $req.method = "POST"
  40. $req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  41. $req.Headers.Add("Accept-Language: en-US")
  42. $req.Headers.Add("Accept-Encoding: gzip,deflate")
  43. $req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  44. $req.AllowAutoRedirect = $false
  45. $req.ContentType = "application/x-www-form-urlencoded"
  46. $req.ContentLength = $buffer.length
  47. $req.TimeOut = 50000
  48. $req.KeepAlive = $true
  49. $req.Headers.Add("Keep-Alive: 300");
  50. $reqst = $req.getRequestStream()
  51. $reqst.write($buffer, 0, $buffer.length)
  52. $reqst.flush()
  53. $reqst.close()
  54. [net.httpWebResponse] $res = $req.getResponse()
  55. $resst = $res.getResponseStream()
  56. $sr = new-object IO.StreamReader($resst)
  57. $result = $sr.ReadToEnd()
  58. $res.close()
  59.  
  60. #Invoke-WebRequest `
  61. # -Body (ConvertTo-Json -Compress -InputObject $payload) `
  62. # -Method Post `
  63. #-Uri "https://hooks.slack.com/services/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" | Out-Null
  64.  
  65. }
  66. ############################Define Variables for server & services##############
  67. $ServicesList = "winlogbeatEnigma, heartbeatEnigma, Sysmon64"
  68. $machineName = Hostname
  69. ##############################################################################
  70. ################################ADD HTML Content#############################
  71.  
  72.  
  73. Add-Content $report "<html>"
  74. Add-Content $report "<head>"
  75. Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
  76. Add-Content $report '<title>Service Status Report</title>'
  77. add-content $report '<STYLE TYPE="text/css">'
  78. add-content $report "<!--"
  79. add-content $report "td {"
  80. add-content $report "font-family: Tahoma;"
  81. add-content $report "font-size: 11px;"
  82. add-content $report "border-top: 1px solid #999999;"
  83. add-content $report "border-right: 1px solid #999999;"
  84. add-content $report "border-bottom: 1px solid #999999;"
  85. add-content $report "border-left: 1px solid #999999;"
  86. add-content $report "padding-top: 0px;"
  87. add-content $report "padding-right: 0px;"
  88. add-content $report "padding-bottom: 0px;"
  89. add-content $report "padding-left: 0px;"
  90. add-content $report "}"
  91. add-content $report "body {"
  92. add-content $report "margin-left: 5px;"
  93. add-content $report "margin-top: 5px;"
  94. add-content $report "margin-right: 0px;"
  95. add-content $report "margin-bottom: 10px;"
  96. add-content $report ""
  97. add-content $report "table {"
  98. add-content $report "border: thin solid #000000;"
  99. add-content $report "}"
  100. add-content $report "-->"
  101. add-content $report "</style>"
  102. Add-Content $report "</head>"
  103. Add-Content $report "<body>"
  104. add-content $report "<table width='100%'>"
  105. add-content $report "<tr bgcolor='Lavender'>"
  106. add-content $report "<td colspan='7' height='25' align='center'>"
  107. add-content $report "<font face='tahoma' color='#003399' size='4'><strong>Service Status Report</strong></font>"
  108. add-content $report "</td>"
  109. add-content $report "</tr>"
  110. add-content $report "</table>"
  111.  
  112. add-content $report "<table width='100%'>"
  113. Add-Content $report "<tr bgcolor='IndianRed'>"
  114. Add-Content $report "<td width='10%' align='center'><B>Server Name</B></td>"
  115. Add-Content $report "<td width='50%' align='center'><B>Service Name</B></td>"
  116. Add-Content $report "<td width='10%' align='center'><B>Status</B></td>"
  117. Add-Content $report "</tr>"
  118.  
  119.  
  120. ########################################################################################################
  121.  
  122. ################################## Get Services Status #################################################
  123.  
  124. Function servicestatus ($serviceslist)
  125.  
  126. {
  127.  
  128.  
  129. foreach ($service in $serviceslist)
  130. {
  131.  
  132. $serviceStatus = get-service -ComputerName $machineName -Name $service
  133.  
  134. if ($serviceStatus.status -eq "Running") {
  135.  
  136. Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Green
  137. $svcName = $serviceStatus.name
  138. $svcState = $serviceStatus.status
  139. Add-Content $report "<tr>"
  140. Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B> $machineName</B></td>"
  141. Add-Content $report "<td bgcolor= 'GainsBoro' align=center> <B>$svcName</B></td>"
  142. Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"
  143. Add-Content $report "</tr>"
  144.  
  145. }
  146.  
  147. else
  148. {
  149. Write-Host $machineName `t $serviceStatus.name `t $serviceStatus.status -ForegroundColor Red
  150. if ($alert -eq "yes")
  151. {
  152. $sename = $serviceStatus.name
  153. $sestatus = $serviceStatus.status
  154. Send-Slack($sename, $machinename, $sestatus)
  155. #Send-Email -From $from1 -To $to -subject "Open Critical: $machineName $sename $sestatus" -smtpserver $smtphost
  156. }
  157. $svcName = $serviceStatus.name
  158. $svcState = $serviceStatus.status
  159. Add-Content $report "<tr>"
  160. Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"
  161. Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"
  162. Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"
  163. Add-Content $report "</tr>"
  164.  
  165.  
  166. }
  167.  
  168.  
  169.  
  170. }
  171.  
  172. }
  173.  
  174. ######################
  175. servicestatus $ServicesList
  176.  
  177. ############################################Close HTMl Tables#########################################
  178.  
  179.  
  180. Add-content $report "</table>"
  181. Add-Content $report "</body>"
  182. Add-Content $report "</html>"
  183.  
  184. #####################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement