Core/Public/Wifi/Get-WifiAvailableNetworks.ps1

Function Get-WiFiAvailableNetworks {
    [Alias('v')]
    [OutputType([Windows.Devices.WiFi.WiFiAvailableNetwork])]
    [CmdletBinding()]
    param(
        [Parameter()]
        [int]$Index
    )
    process {
        &$ScriptBlockCheckWifiRadioWarn
        # 1. Getting the name of the adapter list
        $AsyncOp = [Windows.Devices.WiFi.WiFiAdapter]::FindAllAdaptersAsync()
        while ($AsyncOp.Status -ne [Windows.Foundation.AsyncStatus]::Completed) { Start-Sleep -Milliseconds 50 }
        $Adapters = @($AsyncOp.GetResults())

        if ($Adapters.Count -eq 0) { return }

        # 2. Which adapter are we scanning
        $ToScan = @()
        if ($PSBoundParameters.ContainsKey('Index')) {
            if ($Index -lt $Adapters.Count) {
                $ToScan = @($Adapters[$Index])
            } else {
                Write-Error "Index $Index buiten bereik (Totaal: $($Adapters.Count))."
                return
            }
        } else {
            # No Index then scan every adpater
            $ToScan = $Adapters
        }

        # 3. Run Scan for the selected Adapter(s)
        foreach ($Adapter in $ToScan) {
            $TimeStamp = Get-Date
            $null = $Adapter.ScanAsync() #this doesn't return antyhing usefull
            #while ($ScanOp.Status -eq [Windows.Foundation.AsyncStatus]::Completed) { Start-Sleep -Milliseconds 50 }
            while ($Adapter.NetworkReport.Timestamp.Datetime -gt $TimeStamp) {
                Start-sleep -Milliseconds 50 #lets just wait for it to tell the script it has run
            }
            $Adapter.NetworkReport.AvailableNetworks
        }
    }
}



#Export-ModuleMember -Function Search-WifiNetworks

$WiFiIndexCompleter = {
    param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)
    try {
        $AsyncOp = [Windows.Devices.WiFi.WiFiAdapter]::FindAllAdaptersAsync()
        while ($AsyncOp.Status -eq [Windows.Foundation.AsyncStatus]::Started) { Start-Sleep -Milliseconds 10 }
        $Adapters = @($AsyncOp.GetResults())

        for ($i = 0; $i -lt $Adapters.Count; $i++) {
            $Name = $Adapters[$i].NetworkAdapter.NetworkAdapterId
            $CompletionText = $i.ToString()
            $ListItemText = "$i ($Name)"
            $ToolTip = "Scan alleen adapter $($i): $Name"

            [System.Management.Automation.CompletionResult]::new($CompletionText, $ListItemText, 'ParameterValue', $ToolTip)
        }
    } catch {}
}

Register-ArgumentCompleter -CommandName 'Get-WiFiAvailableNetworks' -ParameterName 'Index' -ScriptBlock $WiFiIndexCompleter