-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwlanprofilemanager-ps.ps1
267 lines (217 loc) · 8.4 KB
/
wlanprofilemanager-ps.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#requires -RunAsAdministrator
<#
.SYNOPSIS
Automatically switch wlan profiles
.DESCRIPTION
This script allows you to automatically switch wlan profiles depending on the SSID connected to.
A profile is a set of IP-related parameters: IP, Gateway, DNS...
Profiles are defined in profiles.psd1 file.
You can use profiles.sample.psd1 as a starter.
This script should be run when the computer connects to a wifi access point.
The easiest way to do this is to add a scheduled task to trigger at wlan connection.
This script needs to be run as administrator to be able to modify IP configuration.
See README.md file for more information on how to install the script on your computer.
.INPUTS
None
.OUTPUTS
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
Purpose/Change: Better DNS IP handling in case of errors
Verify configuration at script startup
Added non-zero exit code for error
Added global catch block
Version: 1.1
Author: Indigo744
Creation Date: 25 april 2019
Purpose/Change: Better wlan interface detection
Log file cleaning only *.log file
Version: 1.0
Author: Indigo744
Creation Date: 24 april 2019
Purpose/Change: Initial script release
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
$DEFAULT_PROFILE = "default_profile"
$AUTO_VALUE = "auto"
#---------------------------------------------------------[External Modules]-------------------------------------------------------
Import-Module "./wlanprofilemanager-ps.psm1" -Force
#-----------------------------------------------------------[Execution]------------------------------------------------------------
# Start transcripts
LogTranscriptStart
# Check lock file
if (LockFileExists) {
Exit 1
}
# Check config file
if (!(ConfigProfilesAvailable)) {
Exit 2
}
# Read config file
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
}
# Get WLAN adapter
Write-Host "Checking current wlan connection..."
$wlanAdapter = ItfAutoDetectWLAN
if (!($wlanAdapter)) {
Write-Error "No wlan interface found, aborting"
Exit 4
}
$currentItfAlias = $wlanAdapter.InterfaceAlias
$currentItfIndex = $wlanAdapter.InterfaceIndex
# Get current SSID
$currentSSID = GetCurrentSSID
if (!($currentSSID)) {
Write-Error "No SSID found, aborting"
Exit 5
}
Write-Host " > Found interface $currentItfAlias ($currentItfIndex) connected on SSID $currentSSID"
ItfPrintIpConfig($currentItfIndex)
# Search for profile in conf
$newProfile = $null
if ($config.ContainsKey($currentSSID)) {
Write-Host "Applying $currentSSID profile..."
$profile_applied = $currentSSID
$newProfile = $config[$currentSSID]
} elseif ($config.ContainsKey($DEFAULT_PROFILE)) {
Write-Host "Applying *default* profile..."
$profile_applied = "default"
$newProfile = $config[$DEFAULT_PROFILE]
} else {
Write-Error "No corresponding profile nor default profile found, aborting"
Exit 0
}
$netIpInterface = Get-NetIPInterface -InterfaceIndex $currentItfIndex
$netIPConfiguration = Get-NetIPConfiguration -InterfaceIndex $currentItfIndex
$netIPAddress = Get-NetIPAddress -InterfaceIndex $currentItfIndex
# (netsh wlan show interfaces | select-string SSID | out-string).Trim().Split([Environment]::NewLine)[0].trim().split(":")[1]
# Write lock file
LockFileCreate
try {
$need_restart = $false
$has_changed = $false
# Setting network IP
if ($newProfile.ip -eq $AUTO_VALUE) {
if (($netIpInterface.Dhcp) -eq "Enabled") {
Write-Host " > DHCP already enabled, nothing to do..."
} else {
# Activate DHCP on interface, nothing more to do
Write-Host " > Set DHCP"
Set-NetIPInterface -InterfaceIndex $currentItfIndex -Dhcp Enabled
# Reset conf after enabling DHCP
Write-Host " > Reset conf"
ItfResetIpConf($currentItfIndex)
$need_restart = $true
$has_changed = $true
}
} else {
# Set a static IP address
$ipAdress = [IPAddress]$newProfile.ip
$ipAdressGw = [IPAddress]$newProfile.gateway
$prefixLength = ComputePrefixLengthFromMaskSubnet($newProfile.mask)
if (($ipAdress -eq $netIPConfiguration.IPv4Address.IPv4Address) -and
($ipAdressGw -eq $netIPConfiguration.IPv4DefaultGateway.NextHop) -and
($prefixLength -eq $netIPAddress.PrefixLength))
{
Write-Host " > Conf already set, nothing to do..."
} else {
# Reset conf before adding more IP/Gw
Write-Host " > Reset conf"
ItfResetIpConf($currentItfIndex)
# Set Conf
Write-Host " > Disable DHCP"
Set-NetIPInterface -InterfaceIndex $currentItfIndex -Dhcp Disabled
Write-Host " > Set $ipAdress/$prefixLength (gw $ipAdressGw)"
New-NetIPAddress –InterfaceIndex $currentItfIndex -AddressFamily IPv4 -IPAddress $ipAdress –PrefixLength $prefixLength -DefaultGateway $ipAdressGw | out-null
Write-Host " > Done"
$need_restart = $true
$has_changed = $true
}
}
# Setting network DNS
if ($newProfile.dns -eq $AUTO_VALUE) {
if (ItfCheckIfDnsIsAuto($currentItfIndex)) {
Write-Host " > DNS already set to auto, nothing to do..."
} else {
# Reset DNS on interface, nothing more to do
Write-Host " > Set automatic DNS"
Set-DnsClientServerAddress -InterfaceIndex $currentItfIndex -ResetServerAddresses
Write-Host " > Done"
$has_changed = $true
}
} else {
# Set static DNS IP addresses
$ipAdressDns = [IPAddress]$newProfile.dns
$ipAdressDnsAlt = [IPAddress]$newProfile.dns_alternate
# Try to get current DNS IP configuration
try { $currentIpAdressDns = $netIPConfiguration.DNSServer.ServerAddresses[0] }
catch { $currentIpAdressDns = "none" }
try { $currentIpAdressDnsAlt = $netIPConfiguration.DNSServer.ServerAddresses[1] }
catch { $currentIpAdressDnsAlt = "none" }
if (($ipAdressDns -eq $currentIpAdressDns) -and ($ipAdressDnsAlt -eq $currentIpAdressDnsAlt))
{
Write-Host " > DNS already set, nothing to do..."
} else {
Write-Host " > Set DNS $ipAdressDns/$ipAdressDnsAlt"
Set-DnsClientServerAddress -InterfaceIndex $currentItfIndex -ServerAddresses ($ipAdressDns,$ipAdressDnsAlt)
Write-Host " > Flush DNS"
Clear-DnsClientCache
Write-Host " > Done"
$has_changed = $true
}
}
if ($has_changed) {
InvokeNotifyTask "Applied profile $profile_applied on $currentItfAlias"
}
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)
} else {
Write-Host " > Skipping restarting interface"
}
} else {
Write-Host " > Interface does not need to be restarted"
}
if ($has_changed) {
Start-Sleep 1
# Print config
ItfPrintIpConfig($currentItfIndex)
}
Write-Host ""
Write-Host "Done."
LogTranscriptCleanOld
}
catch {
Write-host -Foreground Red -Background Black "Fatal exception during script:"
Write-Host -Foreground DarkRed -Background Black $_
Exit 666
}
finally {
# Remove lock file
LockFileRemove
# Remove notify file
NotifyFileRemove
# Stop transcript
LogTranscriptStop
}
Exit 0