-
Notifications
You must be signed in to change notification settings - Fork 8
/
wlanprofilemanager-ps.psm1
466 lines (376 loc) · 11.6 KB
/
wlanprofilemanager-ps.psm1
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#-------------------------------------------------------[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]----------------------------------------------------------
Set-Variable LOCK_FILEPATH -option ReadOnly -Scope Script -Force -value (Join-Path $PSScriptRoot '.lock')
Set-Variable NOTIFY_FILEPATH -option ReadOnly -Scope Script -Force -value (Join-Path $PSScriptRoot '.notify')
Set-Variable LOG_PATH -option ReadOnly -Scope Script -Force -value (Join-Path $PSScriptRoot 'logs')
Set-Variable LOG_HOURS_TO_KEEP -option ReadOnly -Scope Script -Force -value 2
Set-Variable PROFILES_FILENAME -option ReadOnly -Scope Script -Force -value 'profiles.psd1'
Set-Variable TASK_NOTIFY_PATH -option ReadOnly -Scope Script -Force -value "\wlanprofilemanager"
Set-Variable TASK_NOTIFY_NAME -option ReadOnly -Scope Script -Force -value "wlanprofilemanager-notify"
#--------------------------------------------------------[Local variables]---------------------------------------------------------
Set-Variable currentLogFile -Scope Script -Force -value $null
#-----------------------------------------------------------[Functions]------------------------------------------------------------
<#
.SYNOPSIS
Starts a log file in .\logs subdirectory with a datetime name.
.INPUTS
None
.OUTPUTS
None
#>
Function LogTranscriptStart() {
# Create logs dir if not exists
If(!(test-path $script:LOG_PATH))
{
New-Item -ItemType Directory -Force -Path $script:LOG_PATH
}
$formattedDate = Get-Date -format "yyyyMMdd-HH\hmm\mss\s"
$script:currentLogFile = Join-Path $script:LOG_PATH "$formattedDate.log"
# Start transcript and print date
Start-Transcript -path $script:currentLogFile
Get-Date
Write-Host ""
}
<#
.SYNOPSIS
Gets the current log filename
.INPUTS
None
.OUTPUTS
[string] log filename
#>
Function LogTranscriptGetFilename() {
$script:currentLogFile
}
<#
.SYNOPSIS
Remove log file older than a specified amount of hours
.INPUTS
None
.OUTPUTS
None
#>
Function LogTranscriptCleanOld() {
# Delete log file older than X hours
$limit = (Get-Date).AddHours((-1 * $Script:LOG_HOURS_TO_KEEP))
Get-ChildItem (Join-Path $script:LOG_PATH "*.log") | Where-Object {
-not $_.PSIsContainer -and $_.CreationTime -lt $limit
} | Remove-Item
}
<#
.SYNOPSIS
Stop the logging
.INPUTS
None
.OUTPUTS
None
#>
Function LogTranscriptStop() {
# Print date and stop transcript
Write-Host ""
Get-Date
Stop-Transcript
}
<#
.SYNOPSIS
Check if the lock file exists and has not expired (i.e. older than a minute)
.INPUTS
None
.OUTPUTS
[bool] $true if exists, $false otherwise
#>
Function LockFileExists() {
if (Test-Path $script:LOCK_FILEPATH) {
# Will ignore if older than a minute
if ((((Get-Date) - (Get-Item $script:LOCK_FILEPATH).CreationTime)) -gt (New-TimeSpan -minutes 1)) {
Write-Error "Lock file found, but too old. Continuing"
} else {
Write-Error "Lock file found, aborting"
return $true
}
}
return $false
}
<#
.SYNOPSIS
Create the lock file
.INPUTS
None
.OUTPUTS
None
#>
Function LockFileCreate() {
"locked" | Out-File $script:LOCK_FILEPATH
}
<#
.SYNOPSIS
Remove the lock file
.INPUTS
None
.OUTPUTS
None
#>
Function LockFileRemove() {
Remove-Item $script:LOCK_FILEPATH -Force -confirm:$false 2>&1 | out-null
}
<#
.SYNOPSIS
Set the content of the notify file
This file is used for the 'notify' task (passing data from a system process to a user process)
.INPUTS
None
.OUTPUTS
None
#>
Function NotifyFileSet($content) {
$content | Out-File $script:NOTIFY_FILEPATH
}
<#
.SYNOPSIS
Get the content of the notify file
This file is used for the 'notify' task (passing data from a system process to a user process)
.INPUTS
None
.OUTPUTS
[string] file content
#>
Function NotifyFileGet() {
if (Test-Path $script:NOTIFY_FILEPATH) {
return Get-Content -Path $script:NOTIFY_FILEPATH
}
}
<#
.SYNOPSIS
Remove the notify file
This file is used for the 'notify' task (passing data from a system process to a user process)
.INPUTS
None
.OUTPUTS
None
#>
Function NotifyFileRemove() {
Remove-Item $script:NOTIFY_FILEPATH -Force -confirm:$false 2>&1 | out-null
}
<#
.SYNOPSIS
Check if the config file containing the profiles is available
.INPUTS
None
.OUTPUTS
[bool] $true if available, $false otherwise
#>
Function ConfigProfilesAvailable() {
if (!(Test-Path (Join-Path $PSScriptRoot $script:PROFILES_FILENAME))) {
Write-Error "No config file found, aborting"
return $false
}
return $true
}
<#
.SYNOPSIS
Read the config file containing the profiles
.INPUTS
None
.OUTPUTS
[array] Array of profiles
#>
Function ConfigProfilesRead() {
return Import-LocalizedData -BaseDirectory $PSScriptRoot -FileName $script:PROFILES_FILENAME
}
<#
.SYNOPSIS
Verify the config profiles
.INPUTS
[object] Config object
.OUTPUTS
[bool] $true if good, $false otherwise
#>
Function ConfigProfilesVerify($config) {
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 ((($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 ((($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
.INPUTS
None
.OUTPUTS
Microsoft.Management.Infrastructure.CimInstance
Microsoft.Management.Infrastructure.CimInstance#ROOT/StandardCimv2/MSFT_NetAdapter
#>
Function ItfAutoDetectWLAN() {
return Get-NetAdapter |
Where-Object { ($_.PhysicalMediaType -eq 'Native 802.11') -or ($_.PhysicalMediaType -eq 'Wireless LAN') -or ($_.PhysicalMediaType -eq 'Wireless WAN') } |
Select-Object -first 1
}
<#
.SYNOPSIS
Print the current IP configuration of an interface
.PARAMETER $itf_index
Index of the wlan interface to get IP configuration from
.INPUTS
None
.OUTPUTS
None
#>
Function ItfPrintIpConfig($itf_index) {
Write-Host (Get-NetIPConfiguration -InterfaceIndex $itf_index | Format-List | Out-String).Trim()
}
<#
.SYNOPSIS
Wait for an interface to be up (check every second)
.PARAMETER $itf_index
Index of the wlan interface to wait for
.PARAMETER $timeout_s
Max amount of time in seconds to wait before throwing a Timeout exception
Optional, default is $false (no timeout)
.INPUTS
None
.OUTPUTS
None
#>
Function ItfWaitForUpStatus($itf_index, $timeout_s = $false) {
$counter = 0
do {
Write-Host "." -NoNewline
Start-Sleep 1
if ($timeout_s) {
$counter++
if ($counter -gt $timeout_s) {
throw [System.TimeoutException] "Interface $itf_index not up within $timeout_s seconds."
}
}
} while((Get-NetAdapter -InterfaceIndex $itf_index).Status -ne "Up")
Write-Host "!"
}
<#
.SYNOPSIS
Reset the IP configuration of an interface
.PARAMETER $itf_index
Index of the wlan interface to reset
.INPUTS
None
.OUTPUTS
None
#>
Function ItfResetIpConf($itf_index) {
# Remove current IP config
Remove-NetIPAddress -InterfaceIndex $itf_index -confirm:$false 2>&1 | out-null
# Remove current Gateway config
Remove-NetRoute -InterfaceIndex $itf_index -confirm:$false 2>&1 | out-null
}
<#
.SYNOPSIS
Check if DNS of an interface is given by DHCP (auto) or manually set by user
.PARAMETER $itf_index
Index of the wlan interface to wait for
.INPUTS
None
.OUTPUTS
[bool] $true if auto, $false otherwise
#>
Function ItfCheckIfDnsIsAuto($itf_index) {
# https://stackoverflow.com/a/17819465/8861729
$InterfaceGuid = (Get-NetAdapter -InterfaceIndex $itf_index).InterfaceGuid
return ((Get-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$InterfaceGuid").NameServer -eq "")
}
<#
.SYNOPSIS
Get the SSID of the wireless interface
.NOTES
The SSID could be found using (Get-NetConnectionProfile).Name
However, this value may be set to "Unindentified Network" when the IP configuration is invalid
Hence, we can only rely on the "netsh" commandline utility and manual string parsing.
.INPUTS
None
.OUTPUTS
[string] current SSID
#>
Function GetCurrentSSID() {
$wlaninterface = netsh wlan show interfaces
$raw_ssid = ($wlaninterface | select-string SSID | out-string).Trim().Split([Environment]::NewLine)[0].Trim()
return $raw_ssid.split(":")[1].Trim()
}
<#
.SYNOPSIS
Compute the prefix length from a mask subnet
.PARAMETER $maskSubnet
The mask subnet (e.g. 255.255.255.0)
.INPUTS
None
.OUTPUTS
[int] Prefix length
#>
Function ComputePrefixLengthFromMaskSubnet($maskSubnet) {
return ([System.Collections.BitArray](([IPAddress]$maskSubnet).GetAddressBytes()) -ne $false ).Count
}
<#
.SYNOPSIS
Run the notify task to display a message to the current user
.PARAMETER $text
The message to display to the user
.INPUTS
None
.OUTPUTS
None
#>
function InvokeNotifyTask($text)
{
NotifyFileSet $text
Start-ScheduledTask -TaskPath $script:TASK_NOTIFY_PATH -TaskName $script:TASK_NOTIFY_NAME -ErrorAction Ignore
}