0

I have a URL health-checking PowerShell script which correctly gets an HTTP 200 status code on most of my intranet sites, but a '0' status code is returned on a small minority of them. The '0' code is an API return rather than from the web site itself, according to my research of questions from others who have written similar URL-checking PowerShell scripts. Thinking this must be a timeout issue, where API returns '0' before the slowly-responding web site returns its 200, I've researched yet more questions about this subject area on SO and implemented a suggestion from someone to insert a timeout in the script. The timeout setting though, no matter how high I set the timeout value, doesn't help. I still get the same '0' "response" code from the same web sites even though those web sites are up and running as checked from any regular web browser. Any thoughts on how I could tweak the timeout setting in the script below in order to get the correct 200 response code?

The Script:

$URLListFile = "C:\Users\Admin1\Documents\Scripts\URL Check\URL_Check.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue

#if((test-path $reportpath) -like $false)
#{
#new-item $reportpath -type file
#}

#For every URL in the list
$result = foreach($Uri in $URLList) {
    try{
        #For proxy systems
        [System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
        [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

        #Web request
        $req = [system.Net.WebRequest]::Create($uri)
        $req.Timeout=5000
        $res = $req.GetResponse()
    }
    catch {
        #Err handling
        $res = $_.Exception.Response
    }
    $req = $null

    #Getting HTTP status code
    $int = [int]$res.StatusCode

    # output a formatted string to capture in variable $result
    "$int - $uri"

    #Disposing response if available
    if($res){
        $res.Dispose()
    }
}

# output on screen
$result

#output to log file
$result | Set-Content -Path "C:\Users\Admin1\Documents\Scripts\z_Logs\URL_Check\URL_Check_log.txt" -Force

Current output:

200 - http://192.168.1.1/ 
200 - http://192.168.1.2/ 
200 - http://192.168.1.250/config/authentication_page.htm
0 - https://192.168.1.50/ 
200 - http://app1-vip-http.dev.local/ 
0 - https://CA/certsrv/Default.asp
T-Heron
  • 5,385
  • 7
  • 26
  • 52

1 Answers1

1

Perhaps using PowerShell cmdlet Invoke-WebRequest works better for you. It has many more parameters and switches to play around with like ProxyUseDefaultCredentials and DisableKeepAlive

$pathIn  = "C:\Users\Admin1\Documents\Scripts\URL Check\URL_Check.txt"
$pathOut = "C:\Users\Admin1\Documents\Scripts\z_Logs\URL_Check\URL_Check_log.txt"
$URLList = Get-Content -Path $pathIn

$result = foreach ($uri in $URLList) {
    try{
        $res = Invoke-WebRequest -Uri $uri -UseDefaultCredentials -UseBasicParsing -Method Head -TimeoutSec 5 -ErrorAction Stop
        $status = [int]$res.StatusCode
    }
    catch {
        $status = [int]$_.Exception.Response.StatusCode.value__
    }
    # output a formatted string to capture in variable $result
    "$status - $uri"
}

# output on screen
$result

#output to log file
$result | Set-Content -Path $pathOut -Force
Theo
  • 57,719
  • 8
  • 24
  • 41
  • 1
    Very nice. It appears to be working better now with this new code. The only sites I get a '0' return on now are just two of them - and they both use self-signed certificates so the cert error they are generating is likely the only remaining issue. I had incorrectly diagnosed this as a timeout issue for *all* sites when in fact timeout was only an issue for some of them. – T-Heron Jan 10 '21 at 17:19
  • 1
    I'm marking this as answered - the code you suggested cleaned up issues related to the timeout. Regarding bypassing certificate errors, there is a new switch available in PS 6.0 to allow you to do that: *-SkipCertificateCheck* but that isn't available until PS 6.0 and I'm running PS 5.1. I'll need to update. Ref: https://stackoverflow.com/questions/46855241/ignoring-self-signed-certificates-from-powershell-invoke-restmethod-doesnt-work – T-Heron Jan 10 '21 at 21:44