hotspot
param(
[int]$IntervalSeconds = 2,
[switch]$Once
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Get-HostedNetworkData {
try {
$raw = netsh wlan show hostednetwork 2>&1
$text = ($raw | Out-String)
}
catch {
return [pscustomobject]@{
Supported = $false
Mode = 'Unknown'
Status = 'Unavailable'
Ssid = '-'
MaxClients = '-'
ClientCount = 0
Clients = @()
Message = "Failed to query hosted network: $($_.Exception.Message)"
}
}
$mode = if ($text -match 'Mode\s*:\s*(.+)') { $matches[1].Trim() } else { 'Unknown' }
$status = if ($text -match 'Status\s*:\s*(.+)') { $matches[1].Trim() } else { 'Unavailable' }
$ssid = if ($text -match 'SSID name\s*:\s*"?([^\r\n"]+)"?') { $matches[1].Trim() } else { '-' }
$maxClients = if ($text -match 'Max number of clients\s*:\s*(.+)') { $matches[1].Trim() } else { '-' }
$count = 0
if ($text -match 'Number of clients\s*:\s*(\d+)') {
$count = [int]$matches[1]
}
$clients = @()
foreach ($line in ($text -split "`r?`n")) {
if ($line -match '([0-9A-Fa-f]{2}(?:-[0-9A-Fa-f]{2}){5})') {
$clients += $matches[1].ToUpper().Replace('-', ':')
}
}
$clients = @($clients | Select-Object -Unique)
$supported = -not ($text -match 'not supported')
[pscustomobject]@{
Supported = $supported
Mode = $mode
Status = $status
Ssid = $ssid
MaxClients = $maxClients
ClientCount = $count
Clients = $clients
Message = ''
}
}
function Get-ArpMap {
$map = @{}
try {
$raw = arp -a 2>&1
foreach ($line in ($raw | Out-String) -split "`r?`n") {
if ($line -match '^\s*(\d+\.\d+\.\d+\.\d+)\s+([0-9A-Fa-f-]{17})\s+\w+') {
$ip = $matches[1]
$mac = $matches[2].ToUpper().Replace('-', ':')
if (-not $map.ContainsKey($mac)) {
$map[$mac] = @()
}
$map[$mac] += $ip
}
}
}
catch {
return @{}
}
return $map
}
function Get-WifiSignal {
try {
$raw = netsh wlan show interfaces 2>&1
$text = ($raw | Out-String)
if ($text -match 'Signal\s*:\s*(\d+)%') {
return [int]$matches[1]
}
}
catch { }
return $null
}
function Write-Line([string]$Text, [ConsoleColor]$Color = [ConsoleColor]::Gray) {
$previous = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = $Color
Write-Host $Text
$Host.UI.RawUI.ForegroundColor = $previous
}
function Render-Dashboard {
param(
[pscustomobject]$Data,
[hashtable]$Arp,
[Nullable[int]]$Signal,
[int]$Interval
)
$statusOn = $Data.Status -match 'Started|Running'
$statusText = if ($statusOn) { 'ONLINE' } else { 'OFFLINE' }
$statusColor = if ($statusOn) { [ConsoleColor]::Green } else { [ConsoleColor]::Red }
Clear-Host
Write-Line '╭──────────────────────────────────────────────────────────────────╮' DarkCyan
Write-Line ('│ Home IoT \| Windows Hotspot Monitor {0} │' -f (Get-Date -Format 'HH:mm:ss').PadLeft(8)) Cyan
Write-Line '╰──────────────────────────────────────────────────────────────────╯' DarkCyan
Write-Host ''
Write-Host -NoNewline 'Status: '
Write-Line $statusText $statusColor
Write-Line ("SSID: {0}" -f $Data.Ssid) White
Write-Line ("Hosted Mode: {0}" -f $Data.Mode) DarkGray
Write-Line ("Clients: {0} / {1}" -f $Data.ClientCount, $Data.MaxClients) White
if ($Signal -ne $null) {
Write-Line ("Wi-Fi Signal: {0}%" -f $Signal) DarkGray
}
if (-not $Data.Supported) {
Write-Host ''
Write-Line 'Hosted network is not supported on this adapter/driver.' Yellow
}
Write-Host ''
Write-Line 'Connected Clients' Cyan
Write-Line '-----------------' DarkCyan
if (@($Data.Clients).Count -eq 0) {
Write-Line 'No client station entries found.' DarkGray
}
else {
$index = 1
foreach ($client in @($Data.Clients)) {
$ips = if ($Arp.ContainsKey($client)) { ($Arp[$client] | Select-Object -Unique) -join ', ' } else { '-' }
Write-Line (("{0,2}. {1} IP: {2}" -f $index, $client, $ips)) Gray
$index++
}
}
Write-Host ''
Write-Line ("Refresh: every {0}s Ctrl+C to exit" -f $Interval) DarkGray
if ($Data.Message) {
Write-Line $Data.Message Yellow
}
}
try {
do {
$data = Get-HostedNetworkData
$arp = Get-ArpMap
$signal = Get-WifiSignal
Render-Dashboard -Data $data -Arp $arp -Signal $signal -Interval $IntervalSeconds
if (-not $Once) {
Start-Sleep -Seconds $IntervalSeconds
}
} while (-not $Once)
}
catch {
Write-Error $_
exit 1
}
Featured
Advertisement
Category
Action