-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateOpenVPNRoutes.ps1
64 lines (49 loc) · 1.43 KB
/
UpdateOpenVPNRoutes.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
$settings = Get-Content "$PSScriptRoot\settings.json" | ConvertFrom-Json
$items = $settings.items;
$hash = @{}
$fileHash = @{}
Write-Host "Resolving hosts..."
foreach ($item in $items) {
$itemHash = @{}
foreach ($hostsItem in $item.hosts) {
$dns = Resolve-DnsName $hostsItem -Type A
foreach ($dnsItem in $dns) {
if ($dnsItem.Type -eq "A") {
if ($hash.Keys -notcontains $dnsItem.IPAddress) {
$hash[$dnsItem.IPAddress] = @()
}
$hash[$dnsItem.IPAddress] += $hostsItem
$itemHash[$dnsItem.IPAddress] = $hash[$dnsItem.IPAddress]
$text = "`n#$hostsItem`nroute $($dnsItem.IPAddress)"
Write-Host $text -NoNewline
}
}
}
foreach ($file in $item.files) {
if ($fileHash.Keys -notcontains $file) {
$fileHash[$file] = @{}
}
foreach ($ip in $itemHash.Keys) {
$fileHash[$file][$ip] = $itemHash[$ip]
}
}
}
foreach ($file in $fileHash.Keys) {
$newRoutes = ""
foreach ($ip in $fileHash[$file].Keys) {
$item = ""
foreach ($h in $fileHash[$file][$ip]) {
$item += "`n#$h"
}
$item += "`nroute $ip"
$newRoutes += $item
}
if (Test-Path $file) {
(Get-Content $file -Raw) -replace '(?s)(#BEGINAUTOROUTES).*?(#ENDAUTOROUTES)', "`$1`n$newRoutes`n`n`$2" | Out-File $file -encoding utf8
Write-Host "`n`n$file has been updated"
} else {
Write-Host "`n`nUnable to update $file because it doesn't exist"
}
}
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')