Skip to content

Commit

Permalink
Add option "_opt_restart_itf"
Browse files Browse the repository at this point in the history
  • Loading branch information
Indigo744 committed Mar 15, 2020
1 parent dd5a7da commit 6f008e6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 37 deletions.
9 changes: 8 additions & 1 deletion profiles.sample.psd1
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
@{
# Option : restart interface
# Set when the script should restart the interface
# - IfNeeded (default): restart if DHCP config changed
# - Always: always restart if something changed
# - Never: never restart interface
_opt_restart_itf = "IfNeeded"

# Default network (will be applied if no match)
default_profile = @{
ip = "auto"
Expand Down Expand Up @@ -33,7 +40,7 @@
dns = "1.1.1.1"
dns_alternate = "9.9.9.9"
}

"WIFI_SSID_NAME_3" = @{
# Set specific IP
ip = "10.0.0.5"
Expand Down
30 changes: 23 additions & 7 deletions wlanprofilemanager-ps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
Log file stored in .\logs
.NOTES
Version: 1.3
Author: Indigo744
Creation Date: 15 March 2020
Purpose/Change: Added option "_opt_restart_itf" to choose when the script should restart interface
(default is "IfNeeded", which is the same behavior as 1.2)
Version: 1.2
Author: Indigo744
Creation Date: 27 august 2019
Expand Down Expand Up @@ -74,6 +80,9 @@ Write-Host "Reading configuration in $PROFILES_FILENAME..."
$config = ConfigProfilesRead
Write-Host " > Found $($config.Count) profiles: $($config.Keys -join ', ')"

# Get options
$optItfRestart = ConfigGetOptionItfRestart $config

# Verify config profiles
if (!(ConfigProfilesVerify $config)) {
Exit 3
Expand Down Expand Up @@ -211,14 +220,21 @@ try {
InvokeNotifyTask "Applied profile $profile_applied on $currentItfAlias"
}

if ($need_restart) {
Write-Host " > Restarting interface"
Restart-NetAdapter -Name $currentItfAlias
Write-Host " > Done"
if ($need_restart -or ($has_changed -and $optItfRestart -eq $OPT_ITFRESTART_ALWAYS)) {
if ($optItfRestart -ne $OPT_ITFRESTART_NEVER)
{
Write-Host " > Restarting interface"
Restart-NetAdapter -Name $currentItfAlias
Write-Host " > Done"

# Wait for interface to be back up
Write-Host " > Waiting interface.."
ItfWaitForUpStatus($currentItfIndex)
# Wait for interface to be back up
Write-Host " > Waiting interface.."
ItfWaitForUpStatus($currentItfIndex)
} else {
Write-Host " > Skipping restarting interface"
}
} else {
Write-Host " > Interface does not need to be restarted"
}

if ($has_changed) {
Expand Down
89 changes: 60 additions & 29 deletions wlanprofilemanager-ps.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

#-------------------------------------------------------[Global constants]----------------------------------------------------------

Set-Variable OPT_ITFRESTART_IFNEEDED -option ReadOnly -Scope Global -Force -value "IfNeeded"
Set-Variable OPT_ITFRESTART_ALWAYS -option ReadOnly -Scope Global -Force -value "Always"
Set-Variable OPT_ITFRESTART_NEVER -option ReadOnly -Scope Global -Force -value "Never"

#-------------------------------------------------------[Local constants]----------------------------------------------------------

Expand Down Expand Up @@ -231,46 +236,72 @@ Function ConfigProfilesRead() {
.OUTPUTS
[bool] $true if good, $false otherwise
#>
Function ConfigProfilesVerify($configProfiles) {
Function ConfigProfilesVerify($config) {

foreach ($profileKey in $configProfiles.Keys) {
foreach ($paramKey in $configProfiles[$profileKey].Keys) {
$value = $configProfiles[$profileKey][$paramKey]
if ($value -ne "auto")
{
try {
# If param not auto, then it should be a valid IP address
# This dummy cast will throw an exception in case of invalid IP
$value = [IPAddress]$value
}
catch {
Write-Error "Error in profile ${profileKey}: Invalid value `"$value`" set for `"$paramKey`""
return $false
foreach ($key in $config.Keys) {
if ($key.StartsWith("_opt_")) {
# This is an option, not a profile
} else {
foreach ($paramKey in $config[$key].Keys) {
$value = $config[$key][$paramKey]
if ($value -ne "auto")
{
try {
# If param not auto, then it should be a valid IP address
# This dummy cast will throw an exception in case of invalid IP
$value = [IPAddress]$value
}
catch {
Write-Error "Error in profile ${profileKey}: Invalid value `"$value`" set for `"$paramKey`""
return $false
}
}
}
}

# Check if IP or not mask or gateway is DHCP and not the others
if ((($configProfiles[$profileKey].ip -eq "auto") -or ($configProfiles[$profileKey].mask -eq "auto") -or ($configProfiles[$profileKey].gateway -eq "auto")) -and
(($configProfiles[$profileKey].ip -ne "auto") -or ($configProfiles[$profileKey].mask -ne "auto") -or ($configProfiles[$profileKey].gateway -ne "auto")))
{
Write-Error "Error in profile ${profileKey}: ip, mask and gateway must be set to DHCP together"
return $false
}
# Check if IP or not mask or gateway is DHCP and not the others
if ((($config[$key].ip -eq "auto") -or ($config[$key].mask -eq "auto") -or ($config[$key].gateway -eq "auto")) -and
(($config[$key].ip -ne "auto") -or ($config[$key].mask -ne "auto") -or ($config[$key].gateway -ne "auto")))
{
Write-Error "Error in profile ${profileKey}: ip, mask and gateway must be set to DHCP together"
return $false
}

# Check if DNS or alternative DNS is DHCP and not the other
if ((($configProfiles[$profileKey].dns -eq "auto") -and ($configProfiles[$profileKey].dns_alternate -ne "auto")) -or
(($configProfiles[$profileKey].dns -ne "auto") -and ($configProfiles[$profileKey].dns_alternate -eq "auto")))
{
Write-Error "Error in profile ${profileKey}: dns and dns_alternate must be set to DHCP together"
return $false
}
# Check if DNS or alternative DNS is DHCP and not the other
if ((($config[$key].dns -eq "auto") -and ($config[$key].dns_alternate -ne "auto")) -or
(($config[$key].dns -ne "auto") -and ($config[$key].dns_alternate -eq "auto")))
{
Write-Error "Error in profile ${profileKey}: dns and dns_alternate must be set to DHCP together"
return $false
}

}
}

return $true
}

<#
.SYNOPSIS
Retrieve the option _opt_restart_itf from configuration file
.INPUTS
[object] Config object
.OUTPUTS
[string] the value
#>
Function ConfigGetOptionItfRestart($config) {
if ($config.ContainsKey("_opt_restart_itf")) {
if ($config["_opt_restart_itf"] -in @($OPT_ITFRESTART_IFNEEDED, $OPT_ITFRESTART_ALWAYS, $OPT_ITFRESTART_NEVER)) {
return $config["_opt_restart_itf"]
} else {
Write-Error "Wrong value for _opt_restart_itf: ${config["_opt_restart_itf"]}"
}
}

return $OPT_ITFRESTART_IFNEEDED
}

<#
.SYNOPSIS
Auto detect and get the first WLAN interface of this computer
Expand Down

0 comments on commit 6f008e6

Please sign in to comment.