| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- param(
- $Uri = "http://localhost:8080",
- $ServiceName = "tomcat8"
- )
- $Date = Get-Date
- mkdir -Force ".\logs" | Out-Null
- $logfile = ".\logs\Monitor_http_{0}-{1}-{2}.log" -f ($Date.Year,$Date.Month,$Date.Day)
- Function main(){
- $Date.ToString()
- try {
- $Response = Invoke-WebRequest -UseBasicParsing -URI $Uri
- $success = $true
- } catch {
- $_.exception
- $success = $false
- }
- if ((-not $success) -or $Response.StatusCode -ne 200){
- "Stopping Service ${ServiceName}"
- net stop $ServiceName
- "Stopped Service ${ServiceName}"
- sleep 20 | Out-Null
- "Starting Service ${ServiceName}"
- net start $ServiceName
- "Started Service ${ServiceName}"
- } else {
- "Service ${ServiceName} is up and running..."
- }
- }
- try {
- main | Tee-Object $logfile -Append
- } catch {
- $_.exception | Tee-Object $logfile -Append
- }
|