public/Invoke-OctaNetworkCleanup.ps1
|
function Get-OctaWifiProfileNames { <# .SYNOPSIS Parses `netsh wlan show profiles` for saved profile names. No dedicated PowerShell cmdlet exists for this (profiles live as per-interface XML files, not a queryable CIM class) - splitting each indented "<label>: <name>" line on its first colon is the long-established, locale-independent-enough community pattern for this, since the label text itself is localized but the "label : value" structure is not. ponytail: verified against synthetic sample output (no WLAN AutoConfig service running on the dev/test machine used to build this - a desktop with no Wi-Fi adapter). Degrades gracefully either way: no colon-formatted lines to match means an empty list, not a crash, same as any other "nothing to do" case elsewhere in Octa. #> [CmdletBinding()] param() $output = netsh wlan show profiles 2>$null $names = @() foreach ($line in $output) { if ($line -match '^\s+.+:\s*(\S.+)$') { $names += $Matches[1].Trim() } } return $names } function Invoke-OctaNetworkCleanup { <# .SYNOPSIS Dry-run shows DNS cache/saved Wi-Fi profiles/ARP state; -FlushDns, -RemoveWifiProfile, -ClearArp act on request. 007 US4. Single-shot tool, no undo (DNS cache and ARP entries regenerate on their own; a removed Wi-Fi profile requires re-entering its password, disclosed before removal). #> [CmdletBinding()] param( [switch]$FlushDns, [string]$RemoveWifiProfile, [switch]$ClearArp ) if (-not $FlushDns -and -not $RemoveWifiProfile -and -not $ClearArp) { $dnsCount = @(Get-DnsClientCache -ErrorAction SilentlyContinue).Count $wifiProfiles = @(Get-OctaWifiProfileNames) $arpCount = @(Get-NetNeighbor -ErrorAction SilentlyContinue | Where-Object { $_.State -ne 'Unreachable' }).Count Write-Host "DNS cache entries: $dnsCount" Write-Host "Saved Wi-Fi profiles: $($wifiProfiles -join ', ')" Write-Host "ARP/neighbor entries: $arpCount" return [pscustomobject]@{ DnsCacheCount = $dnsCount WifiProfiles = $wifiProfiles ArpEntryCount = $arpCount } } if ($FlushDns) { Clear-DnsClientCache return [pscustomobject]@{ Status = 'Success'; Message = 'DNS cache flushed.' } } if ($RemoveWifiProfile) { $known = Get-OctaWifiProfileNames if ($RemoveWifiProfile -notin $known) { return [pscustomobject]@{ Status = 'NotFound'; Message = "Wi-Fi profile not found: $RemoveWifiProfile" } } Write-Host "Removing '$RemoveWifiProfile' means you'll need to re-enter its password to reconnect." netsh wlan delete profile name="$RemoveWifiProfile" | Out-Null return [pscustomobject]@{ Status = 'Success'; Message = "Removed Wi-Fi profile: $RemoveWifiProfile" } } if ($ClearArp) { Get-NetNeighbor -ErrorAction SilentlyContinue | Where-Object { $_.State -ne 'Permanent' } | Remove-NetNeighbor -Confirm:$false -ErrorAction SilentlyContinue return [pscustomobject]@{ Status = 'Success'; Message = 'ARP/neighbor cache cleared.' } } } |