Private/ArgumentCompleters.ps1
|
$ScriptBlockArgumentCompleterWifiReconnectionKind = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) # Gebruik de volledige Enum waarden in een array $PossibleValues = @( [Windows.Devices.WiFi.WiFiReconnectionKind]::Automatic, [Windows.Devices.WiFi.WiFiReconnectionKind]::Manual ) # Filteren op basis van de string-waarde van het Enum object $PossibleValues | Where-Object { $_.ToString() -like "$wordToComplete*" } | ForEach-Object { # Door een CompletionResult te maken, geef je PowerShell de juiste metadata [System.Management.Automation.CompletionResult]::new( $_.ToString(), # De tekst die in de console verschijnt $_.ToString(), # De waarde die daadwerkelijk wordt ingevuld 'ParameterValue', # Het type resultaat $_.ToString() # De tooltip (hover tekst) ) } } $ScriptBlockArgumentCompleterSsid = { param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) # 1. Directe Exit bij Hidden # Als de gebruiker al -Hidden heeft getypt, kunnen we geen SSID's suggereren $LocationAccess = &$ScriptBlockCheckLocationAccess if ($LocationAccess) { return $LocationAccess } $radioStatus = &$ScriptBlockCheckRadioOff if ($radioStatus) { return $radioStatus } if ($fakeBoundParameters.ContainsKey('Hidden')) { return [System.Management.Automation.CompletionResult]::new(" ", "SSID Unknow (Hidden Mode)", 'Text', "With a hidden network you need to enter the SSID manualy") } # --- SMART SCAN --- &$ScriptblockSmartScan $allWifiAdapters = Get-WiFiAvailableAdapter $targetIndices = if ($fakeBoundParameters.ContainsKey('Index')) { $fakeBoundParameters.Index } else { 0..($allWifiAdapters.Count - 1) } $givenBssid = $fakeBoundParameters['Bssid'] # 2. Haal netwerken op (Filtering op BSSID & overslaan van lege SSIDs) $networks = foreach ($i in $targetIndices) { if ($null -eq $allWifiAdapters[$i]) { continue } $allWifiAdapters[$i].NetworkReport.AvailableNetworks | Sort-Object NetworkRssiInDecibelMilliwatts -Descending | Where-Object { # Sla lege/verborgen netwerken over in de SSID-lijst if ([string]::IsNullOrWhiteSpace($_.Ssid)) { return $false } if ($givenBssid) { $cleanGiven = $givenBssid -replace '[^0-9A-Fa-f]' $cleanCurrent = $_.Bssid -replace '[^0-9A-Fa-f]' $cleanCurrent -eq $cleanGiven } else { $true } } } # 3. Bouw de lijst & Sorteer op signaalsterkte $networks | Group-Object Ssid | ForEach-Object { $strongest = $_.Group | Select-Object -First 1 $ssidValue = if ($strongest.Ssid -match ' ') { "`"$($strongest.Ssid)`"" } else { $strongest.Ssid } $displayText = "$($strongest.Ssid) ($($strongest.SignalBars * 20)% / $($strongest.NetworkRssiInDecibelMilliwatts) dBm)" if ($strongest.Ssid -like "$wordToComplete*") { [System.Management.Automation.CompletionResult]::new($ssidValue, $displayText, 'ParameterValue', "BSSID: $($strongest.Bssid)") } } } $ScriptBlockArgumentCompleterBssid = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) $LocationAccess = &$ScriptBlockCheckLocationAccess if ($LocationAccess) { return $LocationAccess } $radioStatus = &$ScriptBlockCheckRadioOff if ($radioStatus) { return $radioStatus } # 1. Voorbereiding $filterSsid = if ($fakeBoundParameters.ContainsKey('Ssid')) { $fakeBoundParameters['Ssid'] -replace '["'']', '' } else { $null } $showHidden = $fakeBoundParameters.ContainsKey('Hidden') # --- SMART SCAN --- &$ScriptblockSmartScan $allAvailableNetworks = Get-WifiAvailableNetworks | Sort-Object NetworkRssiInDecibelMilliwatts -Descending $results = New-Object System.Collections.Generic.List[System.Management.Automation.CompletionResult] $foundMatch = $false # 3. De 'Kook-Logica' (Nauwkeurige Filter) foreach ($net in $allAvailableNetworks) { $isSsidEmpty = [string]::IsNullOrWhiteSpace($net.Ssid) $hasSsidFilter = -not [string]::IsNullOrWhiteSpace($filterSsid) # De 'Match' direct bepalen (First-match-wins door 'break') $isMatch = switch ($true) { ($hasSsidFilter -and $showHidden) { $isSsidEmpty; break } # Scenario 3 ($hasSsidFilter) { ($net.Ssid -eq $filterSsid); break } # Scenario 2 ($showHidden) { $isSsidEmpty; break } # Scenario 1b default { (-not $isSsidEmpty); break } # Scenario 1a } if ($isMatch) { $foundMatch = $true $label = if ($isSsidEmpty) { "$($net.Bssid) [HIDDEN]" } else { "$($net.Bssid) ($($net.Ssid))" } $results.Add([System.Management.Automation.CompletionResult]::new( $net.Bssid, $label, 'ParameterValue', "Signaal: $($net.SignalBars * 20)% ($($net.NetworkRssiInDecibelMilliwatts) dBm) Band ($(Get-WifiBandName $net.ChannelCenterFrequencyInKilohertz))" )) } } # 4. De Fallback (Blokkeert de standaard help van de CLI) if (-not $foundMatch) { # We voegen de spatie toe. Omdat we deze returnen, 'denkt' PS dat er een resultaat is # en stopt hij met het tonen van willekeurige commandline history/help. $results.Add([System.Management.Automation.CompletionResult]::new(" ", "No match in range", 'Text', "No netwerk found with this filter")) } return $results | Where-Object { [string]::IsNullOrEmpty($wordToComplete) -or $_.CompletionText -like "$wordToComplete*" } } $ScriptBlockArgumentCompleterIndex = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) Process { $Wifiadapters = Get-WiFiAvailableAdapter -TimeoutMs 1000 #this should be instant for ($i = 0; $i -lt $Wifiadapters.Count; $i++) { $id = $Wifiadapters[$i].NetworkAdapter.NetworkAdapterId $name = (Get-NetAdapter | Where-Object DeviceID -match $id).InterfaceDescription [System.Management.Automation.CompletionResult]::new($i, $i, 'ParameterValue', "Adapter $i : $name") } } } $ScriptBlockArgumentCompleterRadioState = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) $States = @('On', 'Off') # Meest gebruikte WinRT RadioStates $States | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', "Set radio to $_") } } $ScriptBlockArgumentCompleterDeviceName = { param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) # Haal alle netwerkadapters op $Netadapters = Get-NetAdapter | Where-Object { $_.Name -eq "Wi-Fi" } | Select-Object InterfaceDescription, InstanceID foreach ($Netadapter in $Netadapters) { # Alleen tonen als de naam matcht met wat er al getypt is if ($Netadapter.InterfaceDescription -like "$wordToComplete*") { [System.Management.Automation.CompletionResult]::new( "'$($Netadapter.InterfaceDescription)'", # 1. CompletionText: This is what lands in the function (GUID) $Netadapter.InterfaceDescription, # 2. ListItemText: This is what the user sees 'ParameterValue', # 3. ResultType "Naam: $($Netadapter.InterfaceDescription)" # 4. ToolTip: Extra info at hover ) } } } $ScriptBlockArgumentCompleterProfileName = { param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) # Get the list of Wi-Fi profiles [Windows.Networking.Connectivity.ConnectionProfile[]]$WifiNetworkProfiles = Get-WifiConnectionProfile # Loop through each Wi-Fi profile and match the ProfileName foreach ($WifiNetworkProfile in $WifiNetworkProfiles) { # Match the wordToComplete with ProfileName (case-insensitive) if ($WifiNetworkProfile.ProfileName -like "$($wordToComplete)*") { # Return the matching profile names for autocompletion [System.Management.Automation.CompletionResult]::new( $WifiNetworkProfile.ProfileName, # Displayed completion result $WifiNetworkProfile.ProfileName, # The value to complete 'ParameterValue', # Completion type "ProfileName:$($WifiNetworkProfile.ProfileName)" # Tooltip (optional) ) } } } #this line is just for debugging $null = $ScriptBlockArgumentCompleterWifiReconnectionKind.` $ScriptBlockArgumentCompleterSsid,` $ScriptBlockArgumentCompleterBssid,` $scriptblockArgumentCompleterIndex,` $ScriptBlockArgumentCompleterRadioState,` $ScriptBlockArgumentCompleterDeviceName,` $ScriptBlockArgumentCompleterProfileName |