-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-RoutesToRouteTable.ps1
204 lines (158 loc) · 6.24 KB
/
Add-RoutesToRouteTable.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
<#
.SYNOPSIS
Add routes to a route table.
.PARAMETER ResourceGroupName
Specifies the Resource Group that the route table belongs to.
.PARAMETER RouteTableName
Specifies the name of the route table to update .
.PARAMETER Addresses
Specifies the AddressPrefixes to add to the route table. This list can be in the form of an array of AddressPrefix or an array of objects containing an AddressPrefix property, making it useful for piping in AddressSpace or Subnet arrays from a Virtual Network object.
.PARAMETER NextHopType
Specifies the NextHopType to set for the new routes.
.PARAMETER NextHopIpAddress
Specifies the NextHopIpAddress to set for the new routes.
.PARAMETER Force
If this parameter is specified, the Route Table will be updated without confirmation.
.EXAMPLE
Add-RoutesToRouteTable.ps1 -ResourceGroupName myRg -RouteTableName myRouteTable -Addresses @('10.1.0.0/24', '10.1.1.0/24') -NextHopType VirtualAppliance -NextHopIpAddress '10.3.0.1'
.EXAMPLE
(Get-AzVirtualNetwork -ResourceGroupName myRg -Name myVnet).AddressSpace | Add-RoutesToRouteTable.ps1 -ResourceGroupName myRg -RouteTableName myRouteTable -NextHopType VirtualAppliance -NextHopIpAddress '10.3.0.1'
.EXAMPLE
(Get-AzVirtualNetwork -ResourceGroupName myRg -Name myVnet).Subnets | Add-RoutesToRouteTable.ps1 -ResourceGroupName myRg -RouteTableName myRouteTable -NextHopType VirtualAppliance -NextHopIpAddress '10.3.0.1' -Force
.NOTES
#>
[CmdletBinding(SupportsShouldProcess)]
Param (
[Parameter(Mandatory)]
[string] $ResourceGroupName,
[Parameter(Mandatory)]
[Alias('Name')]
[string] $RouteTableName,
[Parameter(Mandatory, ValueFromPipeline)]
[Alias('Address')]
[array] $Addresses,
[Parameter(Mandatory)]
[ValidateSet('VirtualNetwork','VirtualNetworkGateway','Internet','VirtualAppliance','None')]
[string] $NextHopType,
[Parameter()]
[ipaddress] $NextHopIpAddress
)
############################################################
# main function
BEGIN {
# validate parameters
if ($NextHopType -eq 'VirtualAppliance' -and -not $NextHopIpAddress) {
Write-Error "-NextHopIpAddress is required for -NextHopType of 'VirtualAppliance'"
return
}
# confirm user is logged into subscription
try {
$result = Get-AzContext -ErrorAction Stop
if (-not $result.Environment) {
Write-Error "Please login (Connect-AzAccount) and set the proper subscription (Select-AzSubscription) context before proceeding."
return
}
}
catch {
Write-Error "Please login (Connect-AzAccount) and set the proper subscription (Select-AzSubscription) context before proceeding."
return
}
$ErrorActionPreference = "Stop"
$newRoutes = @()
# get route table
$routeTable = Get-AzRouteTable -ResourceGroupName $ResourceGroupName -Name $RouteTableName
if (-not $routeTable) {
Write-Error "Unable to find route table $ResourceGroupName/$RouteTableName"
return
}
}
PROCESS {
# Set-StrictMode -Version 3
############################################################
function NewRoutes {
param (
$Address
)
$routes = @()
if ($Address.Name) {
$name = $Address.Name + '-'
}
# allows for passing in a Subnet object or AddressSpace object
$addressPrefixes = @()
if ($address.AddressPrefix) {
$addressPrefixes += $address.AddressPrefix
}
if ($address.AddressPrefixes) {
$addressPrefixes += $address.AddressPrefixes
}
foreach ($addressPrefix in $addressPrefixes) {
$routes += [PSCustomObject] @{
Name = $name + $addressPrefix.Replace('/', '_') + '-route'
AddressPrefix = $addressPrefix
}
}
return $routes
}
############################################################
# determine type of array passed in
if ($Addresses -is [string]) {
# passed in a single string (not an array)
$newRoutes += [PSCustomObject] @{
Name = $Addresses.Replace('/', '_') + '-route'
AddressPrefix = $Addresses
}
}
elseif ($Addresses -is [array] -and $Addresses[0] -is [string]) {
# passed in a array of strings
foreach ($addressPrefix in $Addresses) {
$newRoutes += [PSCustomObject] @{
Name = $addressPrefix.Replace('/', '_') + '-route'
AddressPrefix = $addressPrefix
}
}
}
elseif ($Addresses -is [object]) {
# passed in a single object (pipeline passes in a single object at a time)
$newRoutes += NewRoutes -Address $Addresses
}
elseif ($Addresses -is [array] -and $Addresses[0] -is [object]) {
# passed in an array of objects
foreach ($address in $Addresses) {
$newRoutes += NewRoutes -Address $address
}
}
else {
Write-Error "Addresses types not supported: $Addresses"
}
}
END {
# filter out existing routes
$newRoutes = $newRoutes | Where-Object {$routeTable.Routes.AddressPrefix -notcontains $_.AddressPrefix}
# no new routes
if (-not $newRoutes -or $newRoutes.count -eq 0) {
Write-Verbose "No new routes to add."
return
}
# validate each AddressPrefix
foreach ($newRoute in $newRoutes) {
if ($newRoute.AddressPrefix -notmatch '^(?:[0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}$') {
Write-Error "Invalid AddressPrefix $($newRoute.AddressPrefix)"
return
}
}
# confirm addition of routes
$i = 0;
foreach ($newRoute in $newRoutes) {
if ($PSCmdlet.ShouldProcess($RouteTableName, "Add route $($newRoute.Name) with $($newRoute.AddressPrefix), $NextHopType, $NextHopIpAddress")) {
$result = $routeTable | Add-AzRouteConfig -Name $newRoute.Name -AddressPrefix $newRoute.AddressPrefix -NextHopType $NextHopType -NextHopIpAddress $NextHopIpAddress
$i++
}
}
# check for anything to do
if ($i -eq 0) {
Write-Verbose "No new routes added."
return
}
$result = $routeTable | Set-AzRouteTable
Write-Verbose "$($routeTable.ResourceGroupName)/$($routeTable.Name) - $i new route(s) added."
}