Monitor_http.ps1 841 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. param(
  2. $Uri = "http://localhost:8080",
  3. $ServiceName = "tomcat8"
  4. )
  5. $Date = Get-Date
  6. mkdir -Force ".\logs" | Out-Null
  7. $logfile = ".\logs\Monitor_http_{0}-{1}-{2}.log" -f ($Date.Year,$Date.Month,$Date.Day)
  8. Function main(){
  9. $Date.ToString()
  10. try {
  11. $Response = Invoke-WebRequest -UseBasicParsing -URI $Uri
  12. $success = $true
  13. } catch {
  14. $_.exception
  15. $success = $false
  16. }
  17. if ((-not $success) -or $Response.StatusCode -ne 200){
  18. "Stopping Service ${ServiceName}"
  19. net stop $ServiceName
  20. "Stopped Service ${ServiceName}"
  21. sleep 20 | Out-Null
  22. "Starting Service ${ServiceName}"
  23. net start $ServiceName
  24. "Started Service ${ServiceName}"
  25. } else {
  26. "Service ${ServiceName} is up and running..."
  27. }
  28. }
  29. try {
  30. main | Tee-Object $logfile -Append
  31. } catch {
  32. $_.exception | Tee-Object $logfile -Append
  33. }