From 6f008e63d6b0ae033c018066cb83d50688820577 Mon Sep 17 00:00:00 2001 From: Indigo744 Date: Sun, 15 Mar 2020 12:26:28 +0100 Subject: [PATCH] Add option "_opt_restart_itf" --- profiles.sample.psd1 | 9 +++- wlanprofilemanager-ps.ps1 | 30 ++++++++++--- wlanprofilemanager-ps.psm1 | 89 +++++++++++++++++++++++++------------- 3 files changed, 91 insertions(+), 37 deletions(-) diff --git a/profiles.sample.psd1 b/profiles.sample.psd1 index 7a8de48..6d6964e 100644 --- a/profiles.sample.psd1 +++ b/profiles.sample.psd1 @@ -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" @@ -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" diff --git a/wlanprofilemanager-ps.ps1 b/wlanprofilemanager-ps.ps1 index b1067b2..4bdd2dc 100644 --- a/wlanprofilemanager-ps.ps1 +++ b/wlanprofilemanager-ps.ps1 @@ -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 @@ -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 @@ -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) { diff --git a/wlanprofilemanager-ps.psm1 b/wlanprofilemanager-ps.psm1 index 2929a81..f821296 100644 --- a/wlanprofilemanager-ps.psm1 +++ b/wlanprofilemanager-ps.psm1 @@ -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]---------------------------------------------------------- @@ -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