Public/Wifi/Get-WifiNetworkWpsCapability.ps1

class WifiNetworkWpsCapability {
    [string]$Ssid
    [String]$Bssid
    [decimal]$Channel
    [Byte]$SignalBars
    [Object[]]$WpsKinds
    WifiNetworkWpsCapability([string]$Ssid, [String]$Bssid, [decimal]$Channel, [Byte]$SignalBars, [Object[]]$WpsKinds) {
        $this.Ssid = $Ssid
        $this.Bssid = $Bssid
        $this.Channel = $Channel
        $this.SignalBars = $SignalBars
        $this.WpsKinds = $WpsKinds
    }
    
}
Function Get-WpsCapability {
    <#
.SYNOPSIS
    Retrieves WPS (Wi-Fi Protected Setup) capabilities for specified Wi-Fi networks.
 
.DESCRIPTION
    This function scans a list of Wi-Fi networks and checks their WPS capabilities
    using the `GetWpsConfigurationAsync` method. For each network, it returns its
    WPS configuration, supported WPS kinds (PushButton, PIN, NFC), signal strength,
    and the Wi-Fi channel. The function is useful for assessing which WPS methods
    are supported by the available networks.
 
    **Available WPS Methods (WPSKinds)**:
    - **Unknown (0):** Represents an unknown WPS method, typically when the device cannot determine the WPS type.
    - **WPS PIN (1):** The WPS PIN method, where the user provides a PIN to establish a connection.
    - **WPS PushButton (2):** The WPS PushButton method, where users press a physical or virtual button to initiate the connection.
    - **NFC (3):** Near Field Communication (NFC) for connecting devices by tapping them together.
    - **Ethernet (4):** Reserved or not used for Wi-Fi connections, possibly applicable for wired connections.
    - **USB (5):** Also not used for Wi-Fi connections, potentially referring to wired USB-based setups.
 
.PARAMETER NetworkReport
    An array of Wi-Fi network objects to scan for WPS capabilities. These objects
    are typically retrieved using `Get-WifiAvailableNetworks` or similar methods.
 
.EXAMPLE
    Get-WifiNetworkWpsCapability -NetworkReport $networks
    Retrieves WPS capabilities for the networks in the `$networks` array.
 
.NOTES
    - This function may require a restart if the process encounters issues, as certain
      Wi-Fi drivers or processes might become stuck.
    - Debug mode can be enabled using the `Debug` flag to get more detailed output.
    - The function assumes that the device supports `WPS PushButton` and `WPS PIN`,
      but other methods like `NFC`, `Ethernet`, and `USB` may not always be available or supported by all devices.
#>

    [OutputType([Windows.Devices.WiFi.WiFiConnectionMethod])]
    param (
        $WinRtTask,
        $ResultType,
        $Activity,
        $TimeoutMS,
        $ParentId
    )
    return AwaitWithCancel -WinRtTask $WinRtTask -ResultType $Resulttype -Activity $Activity -TimeoutMs $TimeoutMS -ParentId $ParentId
}    
Function Get-WifiNetworkWpsCapability {
    [CmdletBinding()]
    [OutputType('WifiNetworkWpsCapability')]
    param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("WiFiNetwork")]
        [ValidateNotNullOrEmpty()]
        [Object[]]$NetworkReport  # Accept an array of WiFiAvailableNetwork objects
    )
    Begin {
        $Activity = "Get-WifiNetworkWpsCapability"
        Write-Progress -Activity $Activity -id 0
        # Write-Warning "This Function doesn't always work please restart powershell if it doesn't work something might be stuck"
    }
    Process {
        if ($PSBoundParameters.ContainsKey('Debug')) { $DebugPreference = [System.Management.Automation.ActionPreference]::Continue }
        $Adapter = Get-WiFiAvailableAdapter
        ForEach ($WifiNetwork in $NetworkReport) {
            try {
                Write-Debug ("Ssid:{0} with Bssid:{1}" -f $WifiNetwork.Ssid, $WiFiNetwork.Bssid)
                &$ScriptblockSmartScan -ParentId 0 -TimeoutBlock
                $Result = Get-WpsCapability -WinRtTask ($Adapter.GetWpsConfigurationAsync($WifiNetwork))  -ResultType ([Windows.Devices.WiFi.WiFiWpsConfigurationResult]) -TimeoutMS 30000 -Activity ("{0} Ssid:{1}" -f $Activity, $WifiNetwork.Ssid) -ParentId 0
                # 2. Toon resultaat
                if ($null -ne $Result) { 
                    [WifiNetworkWpsCapability]::new( 
                        $WifiNetwork.Ssid,
                        $WifiNetwork.Bssid,
                        (Get-WifiChannel -ChannelCenterFrequencyInKilohertz $WifiNetwork.ChannelCenterFrequencyInKilohertz),
                        $WifiNetwork.SignalBars,
                        ($Result.SupportedWPSKinds  | ForEach-Object { $_.Tostring() })
                    )
                }
            } catch {
                Write-Error "Fout bij scannen van $($WifiNetwork.Ssid)"
            } finally {
                # 3. CRUCIAAL: Korte pauze om de Wi-Fi driver te laten 'ademen'
                Start-Sleep -Milliseconds 100
                #[System.GC]::Collect() # Forceer een kleine opruiming in de loop
            }
        }
    } end {
        #Write-Progress -Activity $ProgressBarName -Completed
    }
}