Skip to content

Instantly share code, notes, and snippets.

@MateuszNad
Last active November 25, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MateuszNad/90e3dbe4fd7cc487c6507e28254fcbfb to your computer and use it in GitHub Desktop.
Save MateuszNad/90e3dbe4fd7cc487c6507e28254fcbfb to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
The function creates the profile and connects to wifi.
.DESCRIPTION
The function creates the profile and connects to wifi.
.EXAMPLE
PS C:\> Connect-ToWifi -SsidName 'WifiTest'
# Supply values for the following parameters:
# Password: ****************
# Profile WifiTest is added on interface Wi-Fi.
# Connection request was completed successfully.
# SsidName : WifiTest
# ProfilName : WifiTest
# Authentication : WPA2PSK
# Encryption : AES
# Connected : True
.EXAMPLE
PS C:\> Connect-ToWifi -SsidName 'WifiTest' -Password (ConvertTo-SecureString 'TrudnePass123' -AsPlainText -Force)
# Profile WifiTest is added on interface Wi-Fi.
# The network specified by profile "WifiTest" is not available to connect.
# SsidName : WifiTest
# ProfilName : WifiTest
# Authentication : WPA2PSK
# Encryption : AES
# Connected : True
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
General notes
#>
function Connect-ToWifi
{
[cmdletbinding()]
param(
[Parameter(Mandatory, Position = 0)]
[string]$SsidName,
[Parameter(Mandatory, Position = 1)]
[securestring]$Password,
[Parameter(Mandatory = $false)]
[string]$ProfileName,
# default - WPA2PSK
[Parameter(Mandatory = $false)]
[ValidateSet('open', 'shared', 'WPA', 'WPAPSK', 'WPA2', 'WPA2PSK')]
[string]$Authentication = 'WPA2PSK',
# default - AES
[Parameter(Mandatory = $false)]
[ValidateSet('none', 'WEP', 'TKIP', 'AES')]
[string]$Encryption = 'AES',
[Parameter(Mandatory = $false)]
[ValidateSet('auto', 'manual')]
[string]$ConnectionMode = 'auto'
)
try
{
if (-not $PSBoundParameters.ContainsKey('ProfileName'))
{
$ProfileName = $SsidName
}
# decrypt the password
$TempPass = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($TempPass)
$WirelessProfile = @'
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>{0}</name>
<SSIDConfig>
<SSID>
<name>{1}</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>{2}</connectionMode>
<MSM>
<security>
<authEncryption>
<authentication>{3}</authentication>
<encryption>{4}</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>false</protected>
<keyMaterial>{5}</keyMaterial>
</sharedKey>
</security>
</MSM>
<MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
<enableRandomization>false</enableRandomization>
</MacRandomization>
</WLANProfile>
'@ -f $ProfileName, $SsidName, $ConnectionMode, $Authentication, $Encryption, $PlainPassword
# Create the temporary file with a profile
$WirelessProfile | Out-File (New-TemporaryFile -OutVariable ProfileFile) -Force
# Add the WiFi profile
Start-Process netsh -ArgumentList "wlan add profile filename=$($ProfileFile.FullName)" -Wait -NoNewWindow
# Connect to the WiFi network
Start-Process netsh -ArgumentList "wlan connect ssid=$SsidName name=$ProfileName" -Wait -NoNewWindow
$Object = [PSCustomObject]@{
SsidName = $SsidName
ProfilName = $ProfileName
Authentication = $Authentication
Encryption = $Encryption
Connected = $true
}
if ($LASTEXITCODE -ne 0)
{
$Object.Connected = $false
}
Write-Output $Object
}
catch
{
Write-Warning $_
}
finally
{
if ($ProfileFile)
{
Remove-Item $ProfileFile
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment