1

What I am trying to do is restart a webpage once it is down. The services don't let us know that it is down. (Poor programming on the creator's side) We only know that the site is down by when it is down. There are no warnings. There are no error codes, it just goes down. I have been working with Invoke-WebRequest.

$URL = "http://test.example.org"
while (!(Invoke-WebRequest -Uri $URL -UseBasicParsing -Method Head -ErrorAction SilentlyContinue)) {
    Send-MailMessage -To "admin@example.org" -Body "$URL is down" -Subject "Site Down" -SmtpServer mail.example.org -From "SiteMonitor@Example.org"   
    Get-Service -ComputerName Example_Server -Name "Website Service" | Stop-Service
    sleep 2
    Get-Service -ComputerName Example_Server -Name "Website Service" | Set-Service -Status Running
    sleep 2
    "$(Get-Date)" >> C:\logs\ExampleDown.txt
    if (Invoke-WebRequest -Uri $URL -UseBasicParsing -Method Head -ErrorAction SilentlyContinue) {
        Send-MailMessage -To "admin@example.org" -Body "$URL is Up" -Subject "Site Up" -SmtpServer mail.example.org -From "SiteMonitor@Example.org"   
    }
}

The only problem I am having is whenever I do the Invoke-WebRequest against the site when it is down, it produces an error message that stops the script from running each time. The error message is about the website cannot be resolved. I thought the while loop would treat it with a boolean and execute the while loop when the error message was presented because an error message is seen as false in if statements for the most part.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
David
  • 891
  • 1
  • 8
  • 18
  • Remove `-ErrorAction SilentlyContinue` and instead set `$ErrorActionPreference = 'SilentlyContinue'`. The parameter only suppresses errors from the cmdlet itself, not status information from the webserver that is interpreted as an error. – Ansgar Wiechers Dec 16 '18 at 23:10
  • As a side note: Don't use `Set-Service` for starting (or stopping) a service, b/c the cmdlet doesn't handle dependencies. Use `Start-Service` instead. [Related](https://stackoverflow.com/a/39811972/1630171). – Ansgar Wiechers Dec 16 '18 at 23:19
  • Setting the preference variable can affect the rest of your script. If that is ok that may be an acceptable solution. – No Refunds No Returns Dec 16 '18 at 23:39
  • Of course you need to change the preference variable back to its original value *after* the section where you want errors suppressed. In case that wasn't obvious. – Ansgar Wiechers Dec 17 '18 at 08:44
  • @AnsgarWiechers I tried the erroractionpreference, however, this did not help. The script still stops as soon as it has an error. Also, the server is windows server 2008 r2, start-service does not start the service. Luckily I'm only dealing with one item and it has no dependances. – David Dec 17 '18 at 13:54
  • Something about your PowerShell instance is fishy. Please start a clean instance from CMD (`powershell.exe -NoExit -NoProfile`) and try again. Does the problem persist there? – Ansgar Wiechers Dec 17 '18 at 17:16

1 Answers1

0

You can try something like this: (no pun intended)

try {
  $response = $null
  $LastError = $null
  $response = Invoke-WebRequest -Uri http://localhost -OtherParameters
} catch  {
  $response = $Null
  $LastError = $_
}
if ($null -eq $response -or $null -ne $lastError) { "an error occured" }
else {
  "$($response.Content)"
}
No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43