Skip to content

Instantly share code, notes, and snippets.

@realslacker
Last active August 29, 2018 21:50
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 realslacker/105ec98fd47fc9de588f9a0b007261a3 to your computer and use it in GitHub Desktop.
Save realslacker/105ec98fd47fc9de588f9a0b007261a3 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Installs a wireless profile from an exported XML file.
.DESCRIPTION
Installs a wireless profile from an exported XML file, stores the version (install date and time) in the registry.
Author: Shannon Brooks
Copyright: 2018 (c) Shannon Brooks
Version: 2018.8.29.1212
.PARAMETER Path
Path to the wireless profile XML file.
.PARAMETER Version
The version to store for the wireless profile. Defaults to current date and time (format: yyyy.MM.dd.hhmm).
.PARAMETER SettingsPath
The registry location to store the installed version. Defaults to HKLM\Software\WPKG\Settings. Always creates a sub-key 'Wi-Fi'.
.PARAMETER NoAutoConnect
Don't autom
.PARAMETER MakePrimary
Should the profile order be set to 1? If multiple profiles are specified only the last one is 1. Defaults to TRUE.
.PARAMETER Force
Install the profile even if the currently installed version is newer.
.EXAMPLE
Get-Item *.xml | .\Install-WiFiProfile.ps1 -Version 1.0 -MakePrimary:$false -Force
.EXAMPLE
.\Install-WiFiProfile.ps1 -Path .\Profile.xml
.LINK
https://gist.github.com/realslacker/105ec98fd47fc9de588f9a0b007261a3
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string[]]
$Path,
[System.Version]
$Version = (Get-Date -Format "yyyy.MM.dd.hhmm"),
[string]
$SettingsPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WPKG\Settings",
[bool]
$AutoConnect = $true,
[bool]
$MakePrimary = $true,
[switch]
$Force
)
begin {
$ErrorActionPreference = 'Continue'
$InformationPreference = 'Continue'
# check settings path
$WiFiSettingsPath = Join-Path $SettingsPath "Wi-Fi Profiles"
if ( -not(Test-Path -Path $WiFiSettingsPath ) ) {
Write-Verbose "Creating registry key '$WiFiSettingsPath'"
New-Item -Path $WiFiSettingsPath -Force > $null
}
# check that WLAN AutoConfig service is running
if ( -not((Get-Service -Name WlanSvc).Status -eq 'Running') ) {
Write-Information "WLAN AutoConfig service is not running, computer may not have wireless adapters, exiting"
$Host.SetShouldExit(0)
exit
}
# get wireless interfaces
$WirelessInterfaces = netsh wlan show interfaces | Select-String -Pattern 'Name\s+:\s+(.*)$' -AllMatches | %{ $_.Matches.Groups[1].Value }
if ( -not( $WirelessInterfaces ) ) {
Write-Information "No wireless interfaces found, exiting"
$Host.SetShouldExit(0)
exit
}
$WirelessInterfaces | ForEach-Object {
Write-Verbose "Found wireless interface '$_'"
}
}
process {
foreach ( $PathItem in $Path ) {
$PathItem = Get-Item -Path (Resolve-Path -Path $PathItem)
# check that profile we are trying to import exists
if ( -not( Test-Path -Path $PathItem ) ) {
Write-Error "Could not find wireless profile settings file!"
$Host.SetShouldExit(132)
exit
} else {
Write-Verbose "Using wireless profile settings file '$($PathItem.Name)'"
}
# parse the profile name from the settings file
try {
$ProfileXml = [xml](Get-Content -Path $PathItem -Raw)
$ProfileName = $ProfileXml.WLANProfile.name
Write-Verbose "Found WLAN profile '$ProfileName' in configuration file"
} catch {
Write-Error "Could not parse profile name, possibly bad XML profile."
$Host.SetShouldExit(153)
exit
}
# check if the profile is installed
if ( netsh.exe wlan show profiles | Select-String ":\s+${ProfileName}$" ) {
Write-Verbose "Found configuration for profile on computer"
# get the current profile version from the registry
$ProfileVersion = [System.Version](Get-ItemProperty -Path $WiFiSettingsPath -Name $ProfileName -ErrorAction SilentlyContinue).$ProfileName
if ( $ProfileVersion ) {
Write-Verbose "Found version $ProfileVersion already installed"
# check if installed profile is the same or newer than the current version
if ( $ProfileVersion -ge $Version -and -not( $Force ) ) {
Write-Warning "Installed profile '$ProfileName' v$ProfileVersion is the same or newer than v$Version being installed, skipping. Use -Force to install anyway."
continue
}
}
}
Write-Verbose "Installing version $Version"
# import the wireless profile
if ( -not( netsh.exe wlan add profile filename="$($PathItem.FullName)" user=all | Select-String "$ProfileName is added" ) ) {
Write-Error "Profile '$ProfileName' was not correctly added!"
$Host.SetShouldExit(190)
exit
} else {
Write-Information "Profile '$ProfileName' v$Version was installed"
}
# set connection mode
if ( $AutoConnect -and -not( netsh.exe wlan set profileparameter name="$ProfileName" connectionmode=auto | Select-String 'updated successfully' ) ) {
Write-Error "Could not set '$ProfileName' to auto-connect!"
$Host.SetShouldExit(204)
exit
} else {
Write-Verbose "Profile '$ProfileName' set to auto-connect"
}
# make the profile the first
if ( $MakePrimary ) {
foreach ( $Interface in $WirelessInterfaces ) {
if ( -not( netsh.exe wlan set profileorder name="$ProfileName" interface"$Interface" priority=1 ) ) {
Write-Error "Could not set '$ProfileName' as primary profile on interface '$Interface'!"
$Host.SetShouldExit(222)
exit
} else {
Write-Verbose "Profile '$ProfileName' set to primary on interface '$Interface'"
}
}
}
# set the version registry key
Set-ItemProperty -Path $WiFiSettingsPath -Name $ProfileName -Value $Version -Force -Confirm:$false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment