-
Notifications
You must be signed in to change notification settings - Fork 10
/
Create-AzureRMNetworkSecurityGroupRulesFromFortigateRoutingTable.ps1
61 lines (48 loc) · 2.09 KB
/
Create-AzureRMNetworkSecurityGroupRulesFromFortigateRoutingTable.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
<#
.SYNOPSIS
Creates inbound and outbound network security rule configs from the text export of a fortigate routing table.
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]$NetworkSecurityGroup,
[Parameter(Mandatory=$true)][string]$routingTablePath,
[switch]$IncludePrivateRFC1918Subnets,
[int]$StartingPriority=1000,
[int]$PriorityInterval=10
)
process {
$rawRoutingTable = get-content $routingtablePath
#Match a CIDR Notation Subnet (111.222.333.0/24, etc.)
$regexCIDR = '\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}/\d{0,2}'
#Convert the table into a list of subnets
$subnets = $rawRoutingTable | select-string -Pattern $regexCIDR | foreach {$PSItem.Matches.Value}
if (!($IncludePrivateRFC1918Subnets)) {
#Remove known internal networks (RFC1918). This is done poorly and matches 172.10-15.x.x and 172.33-39.x.x which are not technically RFC1918.
$subnets = $subnets -notmatch '^10\.'
$subnets = $subnets -notmatch '^172\.[1-3][0-9]\.'
$subnets = $subnets -notmatch '^192\.168\.'
}
$Priority = $StartingPriority
if ($PSCmdlet.ShouldProcess($NetworkSecurityGroup.Name,"Add Inbound Rules generated from $routingTablePath")) {
foreach ($subnet in $subnets) {
write-verbose "Creating Allow-$($subnet -replace "/","n")-Inbound Rule"
Add-AzureRmNetworkSecurityRuleConfig `
-Name "Allow-$($subnet -replace "/","n")-Inbound" `
-access Allow `
-Direction Inbound `
-DestinationAddressPrefix * `
-SourceAddressPrefix $subnet `
-priority $Priority `
-Description "Generated by Script on $($env:computername) at $(get-date -format u)" `
-SourcePortRange "*" `
-DestinationPortRange "*" `
-Protocol "*" `
-NetworkSecurityGroup $NetworkSecurityGroup `
| out-null
$Priority += $PriorityInterval
}
}
if ($PSCmdlet.ShouldProcess($NetworkSecurityGroup.Name,"Commit New Configuration")) {
Set-AzureRmNetworkSecurityGroup -NetworkSecurityGroup $NetworkSecurityGroup
}
}#Process