I have the below code to check website status in PowerShell version 2.
Begin {
$webRequest = [Net.WebRequest]::Create("http://webdesa2:2003/Login.aspx");
}
Process {
try {
if ((($webRequest.GetResponse()).Statuscode) -as [int] -eq 200) {
Write-Host "Site is Up";
$webrequest.GetResponse();
} else {
Write-Host -Fore Red "Site is Down"
}
} catch {
Write-Host -Fore Red "Site is Down"
}
}
But I am getting the below output when executing the above code.
Exception calling "GetResponse" with "0" arguments: "The remote server returned an error: <401> Unauthorized."
This code worked good on websites where authentication is not required. My intention is to check if the site is up or not.