Can someone help me capture only the StatusCode of invoke-webrequest below so that I can determine if a site is up (200) or down (any other code). I think essentially an if else statement will be needed.
NOTE: I don't need the output of invoke-webrequest. This is the code I'm running now with: .websiteCheck.ps1 https://google.com
$url = $args[0]
[Net.ServicePointManager]::SecurityProtocol = "Tls11"
invoke-webrequest -uri $url -DisableKeepAlive -UseBasicParsing -Method head
which returns a lot more info to work with than I need:
StatusCode : 200
StatusDescription : OK
Content :
RawContent : HTTP/1.1 200 OK
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Date: Wed, 20 Feb 2019 05:55:14 GMT
Expires: ...
Forms :
Headers : {[X-XSS-Protection, 1; mode=block], [X-Frame-Options, SAMEORIGIN], [Cache-Control, private,
max-age=0], [Content-Type, text/html; charset=UTF-8]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml :
RawContentLength : 0
I'd just like to do something like:
if StatusCode = 200 write-host "The site is up"
else write-host "The site is down"
This must be based on invoke-webrequest and not something like [Net.HttpWebRequest] $req = [Net.WebRequest]::Create($url) because otherwise it doesn't work properly with url's that do multiple redirects.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…