Skip to content

Instantly share code, notes, and snippets.

@andyzib
Created November 27, 2018 17:17
Show Gist options
  • Save andyzib/53b2789bb2d339c73d11b68bd8e0e944 to your computer and use it in GitHub Desktop.
Save andyzib/53b2789bb2d339c73d11b68bd8e0e944 to your computer and use it in GitHub Desktop.
Function Test-CaptivePortal {
<#
.SYNOPSIS
Tests internet connectivity using Microsoft's capitve portal test site. Returns $true if a captive portal is detected, otherwise false.
.DESCRIPTION
Tests internet connectivity using Microsoft's capitve portal test site. Returns $true if a captive portal is detected, otherwise false.
For more information on Windows Network Awareness, see
* https://docs.microsoft.com/en-us/windows-hardware/drivers/mobilebroadband/captive-portals
* https://blog.superuser.com/2011/05/16/windows-7-network-awareness/
.PARAMETER Duration
Duration in seconds to continue testing. Minimum 1, Maximum 900
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
.NOTES
Author: Andrew Zbikowski <andrew@itouthouse.com>
Change History:
2018-11-27: Initial function development.
.EXAMPLE
if (-Not (Test-CaptivePortal -Duration 120)) {
Write-Host "No Captive Portal Detected!"
Invoke-WebRequest -Uri http://www.example.com
}
if (Test-CaptivePortal -Duration 90) {
Write-Host "Captive Portal Detected! Use your web browser to log in."
} else {
Write-Host "No Captive Portal Detected!"
}
#>
# Enable -Debug, -Verbose Parameters. Write-Debug and Write-Verbose!
[CmdletBinding()]
# Parameters
Param (
[Parameter(Mandatory=$true,
ValueFromPipeline=$false,
Position=0,
HelpMessage="Duration in seconds to continue testing. Minimum 1, Maximum 900")]
[ValidateRange(1,900)]
[int]$Duration
)
$count = 0
Do {
$count++
$Result = Invoke-WebRequest -Uri http://www.msftncsi.com/ncsi.txt
if ($Result.Content -eq "Microsoft NCSI") {
Write-Verbose "Attempt $($count): it appears there is no captive portal."
$false
Break
} else {
Write-Verbose "Attempt $($count): it appears there is a captive portal."
$true
Start-Sleep -Seconds 1
}
} While ($count -lt $Duration)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment