Public/Wifi/Search-WifiNetworks.ps1
|
Function Search-WifiNetworks { <# .SYNOPSIS Scans for available Wi-Fi networks using the specified Wi-Fi adapter. .DESCRIPTION The `Search-WifiNetworks` function scans for available Wi-Fi networks using the specified Wi-Fi adapter. If no adapter is provided, it defaults to the first available Wi-Fi adapter on the system. The function performs a scan, displays progress during the process, and returns the list of available Wi-Fi networks. .PARAMETER WiFiAdapter Optional. The Wi-Fi adapter to use for scanning. If not provided, the function will use the first available Wi-Fi adapter retrieved via `Get-WiFiAvailableAdapter`. .PARAMETER TimeoutMs Optional. The timeout duration (in milliseconds) for scanning. Defaults to 10,000 milliseconds (10 seconds). .EXAMPLE Search-WifiNetworks Scans for available Wi-Fi networks using the first available Wi-Fi adapter. .EXAMPLE Get-WiFiAvailableAdapter | Search-WifiNetworks Scans for available Wi-Fi networks using a specified Wi-Fi adapter. .NOTES - The function will display progress during the network scan. - The results are retrieved from the internal Wi-Fi network cache after scanning completes. #> [OutputType([Windows.Devices.Wifi.WifiAvailableNetwork])] param( # We laten het type vrij of gebruiken de WinRT klasse [Parameter(ValueFromPipeline = $true)] [Windows.Devices.WiFi.WiFiAdapter]$WiFiAdapter, [int]$TimeoutMs = 10000 ) begin { #Use Default if not given if (-not $WiFiAdapter) { $WiFiAdapter = Get-WiFiAvailableAdapter -TimeoutMs $TimeoutMs } } Process { return Search-WifiNetworksPrivate -Wifiadapter $WifiAdapter -TimeoutMS $TimeoutMs } } Function Search-WifiNetworksPrivate { [OutputType([Windows.Devices.Wifi.WifiAvailableNetwork])] param( [Parameter(ValueFromPipeline = $true)] [Windows.Devices.WiFi.WiFiAdapter]$WiFiAdapter, [int]$TimeoutMs = 10000, [int]$ParentId = 0 ) begin { #Use Default if not given if (-not $WiFiAdapter) { $WiFiAdapter = Get-WiFiAvailableAdapter -TimeoutMs $TimeoutMs } } Process { if (-not $WiFiAdapter) { Write-Warning "No Wifi Adapter Found" Return $null } # Build the parameter hashtable $Id = if ($PSBoundParameters.ContainsKey("ParentId") -and $ParentId -gt 0) { $ParentId + 1 } else { 0 } $Activity = "Search Wifi Networks" # Build the parameter hashtable $ProgressParams = @{ Id = $Id Activity = $Activity Status = "Start scanning" PercentComplete = 0 } # Only add ParentId if needed if ($Id -gt 0) { $ProgressParams.ParentId = $ParentId } # Show starting progress Write-Progress @ProgressParams # Perform scan, using the same precomputed ID [Void](AwaitWithCancel -WinRtTask ($WiFiAdapter.ScanAsync()) -ResultType ($null) -TimeoutMs $TimeoutMs -ParentId $Id -Activity "Searching") # Complete progress Write-Progress -Id $Id -Completed -Activity $Activity $script:WiFiLastScanTime = Get-Date #The return is actualy just the internal cash return Get-WifiAvailableNetworks } } |