Public/Add-WLANNetwork.ps1
Function Add-WLANNetwork { <# .SYNOPSIS Add new WLAN Profile .DESCRIPTION Add new WLAN Profile .PARAMETER SSID The SSID .PARAMETER Key The WPA2 PSK Key .PARAMETER XMLConfig The XML Config file (optional) .INPUTS System.String System.IO.FileInfo .EXAMPLE Add-WLANNetwork -SSID "SomeSSID" -Key "S0m3n3tw0rkK3y" .LINK about_functions_advanced .LINK about_CommonParameters #> [CmdletBinding( DefaultParameterSetName = "Arguments", SupportsShouldProcess = $true, ConfirmImpact = "Medium" )] [OutputType('System.String')] Param( [Parameter( ParameterSetName = "Arguments", Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "The SSID" )] [String]$SSID, [Parameter( ParameterSetName = "Arguments", Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "The WPA2 PSK Key" )] [ValidateLength(8, 63)] [String]$Key, [Parameter( ParameterSetName = "XMLConfig", Mandatory = $true )] [System.IO.FileInfo]$XMLConfig ) Begin { $TestPath = Test-Path -Path C:\Operator If ($TestPath -eq $False) { New-Item -Path C:\Operator -ItemType Directory | Out-Null } $basexmlcontent = "<?xml version=`"1.0`"?> <WLANProfile xmlns=`"http://www.microsoft.com/networking/WLAN/profile/v1`"> <name></name> <SSIDConfig> <SSID> <hex></hex> <name></name> </SSID> </SSIDConfig> <connectionType>ESS</connectionType> <connectionMode>auto</connectionMode> <MSM> <security> <authEncryption> <authentication>WPA2PSK</authentication> <encryption>AES</encryption> <useOneX>false</useOneX> </authEncryption> <sharedKey> <keyType>passPhrase</keyType> <protected>false</protected> <keyMaterial></keyMaterial> </sharedKey> </security> </MSM> <MacRandomization xmlns=`"http://www.microsoft.com/networking/WLAN/profile/v3`"> <enableRandomization>false</enableRandomization> <randomizationSeed></randomizationSeed> </MacRandomization> </WLANProfile>" If ($XMLConfig) { $xmlpath = $XMLConfig } Else { $xmlpath = "C:\Operator\Wi-Fi-WLANToAdd.xml" If (Test-Path $xmlpath) { Remove-Item -Path $xmlpath } } [string]$RandomizationSeed = Get-Random -Minimum 100000000 } Process { If (!$XMLConfig) { Add-Content -Path $xmlpath -Value $basexmlcontent } $xml = New-Object -TypeName "XML" Try { If ($PSCmdlet.ShouldProcess($SSID, "Compile WLAN profile data")) { $xml.Load($xmlpath) $xml.WLANProfile.name = $SSID $xml.WLANProfile.SSIDConfig.SSID.name = $SSID $xml.WLANProfile.SSIDConfig.SSID.hex = $(ConvertTo-Hex -Text $SSID) $xml.WLANProfile.MSM.security.sharedKey.keyMaterial = $Key $xml.WLANProfile.MacRandomization.randomizationSeed = $RandomizationSeed $xml.Save($xmlpath) } } Catch { $Error[0] } } End { Start-Sleep -Seconds 1 If ($PSCmdlet.ShouldProcess($SSID, "Add WLAN Profile to computer")) { $runCMD = netsh wlan add profile filename=$xmlpath user=all If ($runCMD -notmatch "Profile $SSID is added on interface") { Throw $runCMD } Return $runCMD } } } |