Skip to content

Instantly share code, notes, and snippets.

@algonzalez
Last active July 8, 2025 20:28
Show Gist options
  • Save algonzalez/85021facb24a1093a2a23cee8ea61b3e to your computer and use it in GitHub Desktop.
Save algonzalez/85021facb24a1093a2a23cee8ea61b3e to your computer and use it in GitHub Desktop.
Call out to various http(s) endpoints to determine if you can access the web.
function Test-IsOnline {
param([Alias('t', 'timeout', 'timeout-secs')][int] $timeout_secs)
$urls = 'https://google.com/generate_204', `
'http://google.com/generate_204', `
'https://edge-http.microsoft.com/captiveportal/generate_204', # not sure if MS supports https?
'http://edge-http.microsoft.com/captiveportal/generate_204', `
'https://connectivity-check.ubuntu.com', `
'https://connectivity-check.ubuntu.com'
$okcodes = '204', '200' # above URLs should always return 204
if ($timeout_secs -lt 0) { $timeout_secs = 0 }
foreach ($url in $urls) {
$response = $(curl --silent --write-out "%{http_code}" --max-time $timeout_secs $url)
if ($response -in $okcodes) { return $true }
}
return $false
}
#set-alias isonline Test-IsOnline
Test-IsOnline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment