Categories: PowerShell

PowerShell – How to check response code from a website

There are times when you need to verify websites availability to be able to tell if it's up or not. Below you can find a simple approach to show whether the site is up or not. This comes useful in situations where website availability is crucial for your business.

Solution
[string] $url = 'https://evotec.xyz'

function Get-WebStatus($url) {
    try {
        [net.httpWebRequest] $req = [net.webRequest]::create($url)
        $req.Method = "HEAD"
        [net.httpWebResponse] $res = $req.getResponse()

        if ($res.StatusCode -eq "200") {
            write-host "`nSite $url is up (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor green
        } else {
            write-host "`nSite $url is down`n" ` -ForegroundColor red
        }
    } catch {

        write-host "`nThings went bad (dns issue?). Try again.`n" ` -ForegroundColor red
    }
}

Get-WebStatus $url

To be able to check for the status of a website quickly and efficiently one can use the following code. This code verifies whether site returns Status Code 200. In case of different code returns it's assumed the website is down.

This post was last modified on November 11, 2018 01:04

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

2 weeks ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 months ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 months ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 months ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 months ago