We are hoping to have a powershell or .net script to automatically connect the user to our wifi network. The wifi profile is populated using netsh wlan add profile filename=”ourWIFI.xml”.
The following code works fine with wpa2 personal at home. it just requires a password and ssid at home. At the office we require a username/password to join the wifi. No matter what i try it returns ConnectionStatus: NetworkNotAvailable. Clicking on the available networks and manually entering Username and Password it connects fine. The following is saved as a ps1 file. If someone has a suggestion i would love to hear from you.
Thanks in advance to everyone who is smarter than me
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]
[Windows.Devices.WiFi.WiFiAdapter,Windows.Devices.WiFi,ContentType = WindowsRuntime]
[Windows.Security.Credentials.PasswordCredential,Windows.Security.Credentials,ContentType=WindowsRuntime]
[Windows.Networking.Connectivity.NetworkSecuritySettings,Windows.Networking.Connectivity,ContentType=WindowsRuntime]
#/ Await is needed to use UWP async methods
#/ Await - can be used to call functions that return an IAsyncOperation,
#/ those that produce a value. It takes the WinRT task object and the type of the output.
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
#/ get list of available wifi adapters
$wiFiAdapterList = Await ([Windows.Devices.Enumeration.DeviceInformation]::FindAllAsync([Windows.Devices.WiFi.WiFiAdapter]::GetDeviceSelector())) ([Windows.Devices.Enumeration.DeviceInformationCollection])
#/ get first wifi adapter
$firstAdapter = Await ([Windows.Devices.WiFi.WiFiAdapter]::FromIdAsync($wiFiAdapterList[0].Id)) ([Windows.Devices.WiFi.WiFiAdapter])
#/ set connection variables
[Windows.Devices.WiFi.WiFiAvailableNetwork]
$availableNetwork = ($firstAdapter.NetworkReport.AvailableNetworks | Where-Object { $_.SSID -match "ourWIFI" })
[Windows.Devices.WiFi.WiFiReconnectionKind]
[ValidateSet("Automatic","Manual")]
$reconnectionKind = "Automatic"
$creds = New-Object Windows.Security.Credentials.PasswordCredential #/ ::new()
$creds.UserName = "Username"
$creds.Password = "Password"
$ssid = "ourWIFI"
#/ Connect to wifi network "ourWIFI", auto reconnect, UN/PW
Await ($firstAdapter.ConnectAsync($availableNetwork, $reconnectionKind, $creds)) ([Windows.Devices.WiFi.WiFiConnectionResult])
Attempted all iterations of the ConnectAsync method including hidden networks. At one point we thought we might have to do something with the Windows.Networking.Connectivity class but are not sure where to start
Rogue is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.