WifiConnectionAPI.psm1
<#
.Synopsis Wifi .Description Manage Wifi connections .Parameter Get-RadioState .Parameter Set-RadioState .Parameter Get-WifiDeviceInformation .Parameter Get-WiFiAvailableAdapter .Parameter Get-WifiConnectionProfile .Parameter Remove-WifiConnectionProfile .Parameter Get-WifiCurrentConection .Parameter Get-WifiAvailableNetworks .Parameter Disconnect-WifiNetwork .Parameter Connect-WiFiNetwork #> ImportSystemModules Add-Type -AssemblyName System.Runtime.WindowsRuntime $asTaskGeneric=([System.WindowsRuntimeSystemExtensions].GetMethods()|?{$_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'})[0] Function Await($WinRtTask, $ResultType){ $asTask=$asTaskGeneric.MakeGenericMethod($ResultType) $netTask=$asTask.Invoke($null, @($WinRtTask)) $netTask.Wait(-1)|Out-Null return $netTask.Result } [Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null [Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null [Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null Function Get-RadioState{ <# .SYNOPSIS Get-RadioState list all available Radios and shows the state of them .EXAMPLE get-RadioState #> [outputType([Windows.Devices.Radios.Radio])] param() if((Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync())([Windows.Devices.Radios.RadioAccessStatus])) -match "Allowed"){ Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync())([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]]) }else{ write-host "No acces to radio" } } Function Set-RadioState{ <# .SYNOPSIS Set-Radiostate use the Get-radioState to list the radios you can switch on or off .REMARK Get-RadioState | Set-RadioState -SetState Off/On #> param( [Parameter(Mandatory=$true)][ArgumentCompleter({ param($commandName,$parameterName,$wordToComplete,$commandAst,$fakeBoundParameters) $possibleValues=@{Values=@([Windows.Devices.Radios.RadioState]::On,[Windows.Devices.Radios.RadioState]::Off)} if($fakeBoundParameters.ContainsKey('Type')){ $possibleValues[$fakeBoundParameters.Type]|?{$_ -like "$wordToComplete*"} }else{ $possibleValues.Values|%{$_} } })]$SetState, [parameter(Mandatory=$true,ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][Windows.Devices.Radios.Radio][alias("Radio")][array]$Radios ) begin { } process{ $Radios.Gettype() Await ($Radios.SetStateAsync($SetState)) ([Windows.Devices.Radios.RadioAccessStatus]) } end{} } [Windows.Devices.WiFi.WiFiAdapter,Windows.Devices.Wifi,ContentType=WindowsRunTime]|Out-Null [Windows.Devices.Enumeration.DeviceInformation,Windows.Devices.Enumeration,ContentType=WindowsRunTime]|Out-Null [Windows.Security.Credentials.PasswordCredential,Windows.Security.Credentials,ContentType=WindowsRunTime]|Out-Null [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,Contenttype=WindowsRuntime]| Out-Null Function Get-WifiDeviceInformation{ [OutputType([Windows.Devices.Enumeration.DeviceInformationCollection])] Param() Await([Windows.Devices.Enumeration.DeviceInformation]::FindAllAsync([Windows.Devices.WiFi.WiFiAdapter]::GetDeviceSelector()))([Windows.Devices.Enumeration.DeviceInformationCollection]) } Function Get-WiFiAvailableAdapter{ #[outputtype([Windows.Devices.WiFi.WiFiAdapter],ParameterSetName="WiFiAvailableAdapter")] [outputtype([Windows.Devices.WiFi.WiFiAdapter])] param() Await([Windows.Devices.WiFi.WiFiAdapter]::FindAllAdaptersAsync())([System.Collections.Generic.IReadOnlyList[Windows.Devices.WiFi.WiFiAdapter]]) } Function Get-WifiConnectionProfile{ [outputType([Windows.Networking.Connectivity.NetworkInformation])] param() [Windows.Networking.Connectivity.NetworkInformation]::GetConnectionProfiles()|?{$_.IsWlanConnectionProfile} } Function Remove-WifiConnectionProfile{ param( [parameter(Mandatory=$true,ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][Windows.Networking.Connectivity.ConnectionProfile] [alias("Profile")] [array]$Profiles ) begin {} process{ if($Profiles.CanDelete){ Write-host ("Delete Profile {0}" -f $Profiles.ProfileName) $Profiles.TryDeleteAsync() | Out-Null }else{ Write-host ("Can't Delete Profile {0}" -f $Profiles.ProfileName) } } end{} } Function Get-WifiCurrentConection{ [outputtype([Windows.Networking.Connectivity.ConnectionProfile])] param() await((Get-WiFiAvailableAdapter).NetworkAdapter.GetConnectedProfileAsync()) ([Windows.Networking.Connectivity.ConnectionProfile]) } Function Disconnect-WifiNetwork{ (Get-WiFiAvailableAdapter).Disconnect() } Function Get-WifiAvailableNetworks{ (Get-WiFiAvailableAdapter).NetworkReport.AvailableNetworks } Function Connect-WiFiNetwork{ [CmdletBinding()] param( [string]$User, [securestring]$password, [parameter(Mandatory=$true)][ArgumentCompleter({ param($commandName,$parameterName,$wordToComplete,$commandAst,$fakeBoundParameters) $possibleValues=@{Values=@(((Get-WiFiAvailableAdapter).NetworkReport.AvailableNetworks)|Select Ssid -Unique|%{if($_.Ssid -match " "){ "`""+$_.ssid+"`""}else{ $_.ssid}}) } if($fakeBoundParameters.ContainsKey('Type')){ $possibleValues[$fakeBoundParameters.Type]|?{$_ -like "$wordToComplete*"} }else{ $possibleValues.Values|%{$_} } })] $Ssid, [string]$Bssid #[parameter(Mandatory=$true,ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][Windows.Devices.WiFi.WiFiAdapter]$WiFiAvailableAdapter ) $WiFiAvailableAdapter=Get-WiFiAvailableAdapter $Credentials=[Windows.Security.Credentials.PasswordCredential]::new() if([bool]$password){ $Credentials.Password=([System.Net.NetworkCredential]::new([string]::empty,$password)).Password } if([bool]$User){ $Credentials.UserName=$User } if(-not $Bssid){ $Network=$WiFiAvailableAdapter.NetworkReport.AvailableNetworks|?{$_.Ssid -match $Ssid}|Sort NetworkRssiInDecibelMilliwatts -Descending|select -First 1 }else{ $Network=$WiFiAvailableAdapter.NetworkReport.AvailableNetworks|?{$_.BSsid -match $BSsid} } $WifiAvailableAdapter.Disconnect() Await($WifiAvailableAdapter.ConnectAsync($Network,[Windows.Devices.WiFi.WiFiReconnectionKind]::Automatic,$Credentials,$null))([Windows.Devices.WiFi.WiFiConnectionResult]) } Export-ModuleMember Get-RadioState Export-ModuleMember Set-RadioState Export-ModuleMember Get-WifiDeviceInformation Export-Modulemember Get-WiFiAvailableAdapter Export-ModuleMember Get-WifiAvailableNetworks Export-ModuleMember Get-WifiConnectionProfile Export-Modulemember Remove-WifiConnectionProfile Export-ModuleMember Get-WifiCurrentConection Export-ModuleMember Disconnect-WifiNetwork Export-ModuleMember Connect-WiFiNetwork |