When there is a need to monitor the set of websites on the local computer or remote server computers it’s easy with a Powershell script, imagine if there are many remote computers one has to check manually then it’s quite difficult for administrators.
Here in this article, we explain how simple code can get any website status on local or remote computers.
$Website = [System.Net.WebRequest]::Create('https://techdiip.com')
# We then get a response from the site.
$HTTP_Response = $Website.GetResponse()
# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
Write-Host "Site is Up and Working as expected!" -BackgroundColor Cyan
}
Else {
Write-Host "The Site is down, please work on it!" -BackgroundColor Red
}
# closing the Http request using the below code.
If ($HTTP_Response -eq $null) {
}
Else { $HTTP_Response.Close() }
Output of the script when the website is accessible.
If the website is not working it returns the output as “The Site is down, please work on it!” in the red color.
How to test website status on multiple remote computers.
We need to create a file for storing all the servers or multiple machines list, I named it as Allservers.txt for and saved it in my local machine c:\temp\Allservers.txt
$Allserers = get-content -path "C:\temp\Allservers.txt"
$Url = "http://www.techdiip.com/"
foreach ($oneserver in $allservers)
{
$website.StatusCode = $null
$website = Invoke-command $oneserver -Scriptblock{param($url)Invoke-WebRequest $url -UseBasicParsing | Select-Object statuscode } -ArgumentList $Url
if ($website.StatusCode -eq '200')
Write-Host "Site is Up and Working as expected!" -BackgroundColor Cyan
}
Else {
Write-Host "The Site is down, please work on it!" -BackgroundColor Red
}
}
The above script tests the website status on multiple remote computers, actually it test only status which means the website is accessible or not, make sure you have admin rights on the remote computers to run this script.
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply