Skip to content

Instantly share code, notes, and snippets.

@Nora-Ballard
Created February 20, 2014 22:37
Show Gist options
  • Save Nora-Ballard/9124761 to your computer and use it in GitHub Desktop.
Save Nora-Ballard/9124761 to your computer and use it in GitHub Desktop.
Functions for testing internet connectivity. Uses the Microsoft NCSI by default, but anything can be passed by parameter.
function Get-ExternalIP
{
$URL = 'http://ifconfig.me/all.xml'
$ProxyURL = [System.Net.WebRequest]::GetSystemWebProxy().GetProxy($URL).AbsoluteUri
$resp = Invoke-WebRequest -Uri $URL -Method Get -Proxy $ProxyURL -ProxyUseDefaultCredentials
$([xml]$resp).info.ip_addr
}
function Test-InternetConnection
{
param(
[string]$URL = 'http://www.msftncsi.com/ncsi.txt'
)
$Proxy = [System.Net.WebRequest]::GetSystemWebProxy().GetProxy($URL)
try {
if ($Proxy.OriginalString -eq $URL) { $HTTPResponse = Invoke-WebRequest -Uri $URL -Method Get }
else { $HTTPResponse = Invoke-WebRequest -Uri $URL -Method Get -Proxy $Proxy.AbsoluteUri -ProxyUseDefaultCredentials}
}
catch { $HTTPResponse = $null }
if ($HTTPResponse.StatusDescription -eq 'OK') {$True} else {$False}
}
function Test-DNSResolution
{
param(
[string]$Name = 'www.msftncsi.com'
)
try { $DNSResponse = [System.Net.Dns]::GetHostEntry($Name) }
catch { $DNSResponse = $null }
If ($DNSResponse) { $True } else { $false }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment