Last active
June 11, 2025 14:00
-
-
Save mheadd/0746524d36f19379c8e1fad3db658f89 to your computer and use it in GitHub Desktop.
This script checks one or more websites and reports their HTTP status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Website Health Checker Script | |
param( | |
[string[]]$Urls = @("https://www.google.com"), | |
[string]$LogFile = $null, | |
[int]$TimeoutSeconds = 30, | |
[switch]$Quiet | |
) | |
# Function to write timestamped log entries | |
function Write-Log { | |
param([string]$Message, [string]$Level = "INFO") | |
if (-not $Quiet) { | |
Write-Host $Message | |
} | |
if ($LogFile) { | |
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
$logEntry = "[$timestamp] [$Level] $Message" | |
Add-Content -Path $LogFile -Value $logEntry | |
} | |
} | |
# Function to check website status | |
function Test-Website { | |
param([string]$Url) | |
try { | |
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew() | |
$response = Invoke-WebRequest -Uri $Url -TimeoutSec $TimeoutSeconds -UseBasicParsing | |
$stopwatch.Stop() | |
$responseTime = $stopwatch.ElapsedMilliseconds | |
$result = [PSCustomObject]@{ | |
Url = $Url | |
StatusCode = $response.StatusCode | |
Status = if ($response.StatusCode -eq 200) { "SUCCESS" } else { "WARNING" } | |
ResponseTime = $responseTime | |
Message = "HTTP $($response.StatusCode) - ${responseTime}ms" | |
Error = $null | |
} | |
return $result | |
} | |
catch { | |
$result = [PSCustomObject]@{ | |
Url = $Url | |
StatusCode = $null | |
Status = "ERROR" | |
ResponseTime = $null | |
Message = $_.Exception.Message | |
Error = $_.Exception.Message | |
} | |
return $result | |
} | |
} | |
# Main execution | |
Write-Log "Website Health Check - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" | |
Write-Log "Checking $($Urls.Count) website$(if($Urls.Count -ne 1){'s'})..." | |
Write-Log "" | |
$results = @() | |
foreach ($url in $Urls) { | |
$result = Test-Website -Url $url | |
$results += $result | |
# Format output based on status | |
switch ($result.Status) { | |
"SUCCESS" { | |
Write-Log "✓ $($result.Url) - $($result.Message)" "SUCCESS" | |
} | |
"WARNING" { | |
Write-Log "⚠ $($result.Url) - $($result.Message)" "WARNING" | |
} | |
"ERROR" { | |
Write-Log "✗ $($result.Url) - $($result.Message)" "ERROR" | |
} | |
} | |
} | |
# Summary | |
Write-Log "" | |
$successCount = ($results | Where-Object { $_.Status -eq "SUCCESS" }).Count | |
$warningCount = ($results | Where-Object { $_.Status -eq "WARNING" }).Count | |
$errorCount = ($results | Where-Object { $_.Status -eq "ERROR" }).Count | |
Write-Log "=== SUMMARY ===" | |
Write-Log "Total sites checked: $($results.Count)" | |
Write-Log "Successful (HTTP 200): $successCount" | |
Write-Log "Warnings (Non-200): $warningCount" | |
Write-Log "Errors (Failed): $errorCount" | |
# Return results for programmatic use | |
return $results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check a single website (default: Google)
.\website_monitor.ps1
Check multiple websites
.\website_monitor.ps1 -Urls "https://google.com", "https://github.com", "https://stackoverflow.com"
Check websites with logging
.\website_monitor.ps1 -Urls "https://google.com", "https://example.com" -LogFile "health_check.log"
Quiet mode (only log to file, no console output)
.\website_monitor.ps1 -Urls "https://google.com" -LogFile "check.log" -Quiet
Custom timeout
.\website_monitor.ps1 -Urls "https://slowsite.com" -TimeoutSeconds 60