Skip to content

Instantly share code, notes, and snippets.

@realslacker
Created August 29, 2018 21:51
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/69a863d4c91c968d6fb568069fb20f3d to your computer and use it in GitHub Desktop.
Save realslacker/69a863d4c91c968d6fb568069fb20f3d to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Test for a wireless profile from an exported XML file or by Name.
.DESCRIPTION
Test for a wireless profile from an exported XML file, checks for a minimum and/or maximum version if supplied.
Author: Shannon Brooks
Copyright: 2018 (c) Shannon Brooks
Version: 2018.8.29.1212
.PARAMETER Path
Path to the wireless profile XML file.
.PARAMETER Name
Name of the wireless profile.
.PARAMETER MaxVersion
The maximum version allowed. If ommitted no max.
.PARAMETER MinVersion
The minimum version allowed. If ommitted no min.
.PARAMETER SettingsPath
The registry location to store the installed version. Defaults to HKLM\Software\WPKG\Settings. Always creates a sub-key 'Wi-Fi'.
.EXAMPLE
Get-Item *.xml | .\Test-WiFiProfile.ps1 -MaxVersion 2.0
.EXAMPLE
.\Test-WiFiProfile.ps1 -Path .\Profile.xml
.LINK
https://gist.github.com/realslacker/6ec32906f3c2e09be65c302c8280216c
#>
[CmdletBinding(DefaultParameterSetName='ByFile')]
param(
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName='ByFile')]
[string[]]
$Path,
[Parameter(Mandatory, ParameterSetName='ByName')]
[string[]]
$Name,
[System.Version]
$MinVersion,
[System.Version]
$MaxVersion,
[string]
$SettingsPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WPKG\Settings"
)
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"
return
}
# 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"
return
}
$WirelessInterfaces | ForEach-Object {
Write-Verbose "Found wireless interface '$_'"
}
}
process {
if ( $PsCmdlet.ParameterSetName -eq 'ByName' ) {
$Items = $Name
} else {
$Items = $Path
}
foreach ( $Item in $Items ) {
if ( $PsCmdlet.ParameterSetName -eq 'ByPath' ) {
$PathItem = Get-Item -Path (Resolve-Path -Path $Item)
# check that profile we are trying to import exists
if ( -not( Test-Path -Path $PathItem ) ) {
Write-Error "Could not find wireless profile settings file!"
} 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."
}
} else {
$ProfileName = $Item
Write-Verbose "Looking for profile '$ProfileName'"
}
# 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
# check if installed profile is newer than the min version
if ( $MinVersion -and ( $ProfileVersion -lt $MinVersion ) ) {
Write-Error "Profile '$ProfileName' v$ProfileVersion is older than v$MinVersion"
$Host.SetShouldExit(-1)
exit
}
# check if installed profile is older than the max version
if ( $MaxVersion -and ( $ProfileVersion -gt $MaxVersion ) ) {
Write-Error "Profile '$ProfileName' v$ProfileVersion is newer than v$MaxVersion"
$Host.SetShouldExit(1)
exit
}
Write-Information "Found profile '$ProfileName'"
$Host.SetShouldExit(0)
exit
} else {
Write-Error "Profile '$ProfileName' not found."
$Host.SetShouldExit(99)
exit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment