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
Use property dereferencesmember access operator and if to do the work.
$uri = $args[0]
[Net.ServicePointManager]::SecurityProtocol = ([Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Ssl3)
try {
if ((Invoke-WebRequest $uri -UseBasicParsing -DisableKeepAlive -Method Head).StatusCode -eq 200) {
Write-Host "The site is up."
}
else {
Write-Host "The site is down."
}
}
catch {
Write-Host "The site is down."
}
The script above has been modified, some comments below are obsolete.