-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSet-MicrosoftAddressObjects.ps1
More file actions
166 lines (152 loc) · 5.44 KB
/
Copy pathSet-MicrosoftAddressObjects.ps1
File metadata and controls
166 lines (152 loc) · 5.44 KB
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
<#
.SYNOPSIS
This script downloads the Microsoft IP and URL address ranges JSON file and generates FortiGate CLI commands to create address objects.
.DESCRIPTION
This script downloads the Microsoft IP and URL address ranges JSON file and generates FortiGate CLI commands to create address objects.
The script uses the Invoke-WebRequest cmdlet to download the JSON file from the Microsoft site.
The script then reads the JSON file and loops through each service area to create IP and URL address objects.
The script uses the Out-File cmdlet to output the FortiGate CLI commands to a file.
The script requires the site parameter to specify the Microsoft site to download the address ranges from.
The available options are:
- USGovGCCHigh
- China
- Worldwide
- USGovDoD
- Germany
The script also includes functions to create IP and URL address objects for FortiGate.
The Create-IP-AddressObjects function creates IP address objects with the specified IP addresses.
The Create-URL-AddressObjects function creates URL address objects with the specified URLs.
The Get-MicrosoftAddressListForFortiGate function downloads the Microsoft IP and URL address ranges JSON file and generates FortiGate CLI commands to create address objects.
The function takes the site parameter as input to specify the Microsoft site to download the address ranges from.
The function outputs the FortiGate CLI commands to a file and displays a success message.
.PARAMETER site
The Microsoft site to download the address ranges from. The available options are:
- USGovGCCHigh
- China
- Worldwide
- USGovDoD
- Germany
.EXAMPLE
Get-MicrosoftAddressListForFortigate -site "Worldwide"
This example downloads the Microsoft IP and URL address ranges JSON file for the Worldwide site and generates FortiGate CLI commands to create address objects.
The FortiGate CLI commands are output to a file and a success message is displayed.
.NOTES
File Name : Set-MicrosoftAddressObjects.ps1
Author : William Ford
Prerequisite : PowerShell V2
#>
function Create-IP-AddressObjects {
param (
[string]$serviceName,
[array]$ips
)
$commands = @()
foreach ($ip in $ips) {
$addressName = "$serviceName`_$ip"
$command = @"
config firewall address
edit 'IP - $addressName'
set subnet $ip
set color 3
next
end
"@
$commands += $command
}
return $commands
}
# Function to create address objects for URLs
function Create-URL-AddressObjects {
param (
[string]$serviceName,
[array]$urls
)
$commands = @()
foreach ($url in $urls) {
$addressName = "$serviceName`_$url"
$command = @"
config firewall address
edit 'FQDN - $addressName'
set type fqdn
set fqdn $url
set color 3
next
end
"@
$commands += $command
}
return $commands
}
# Function to create address groups for IPs and URLs
function Create-AddressGroups {
param (
[string]$serviceName,
[array]$ipAddresses,
[array]$urls
)
$commands = @()
# Create IP address group
if ($ipAddresses.Count -gt 0) {
$ipGroupName = "$serviceName`_IP_Group"
$ipGroupMembers = $ipAddresses | ForEach-Object { "$serviceName`_$_" }
$ipGroupMembersString = $ipGroupMembers -join ' '
$command = @"
config firewall addrgrp
edit '$ipGroupName'
set member $ipGroupMembersString
next
end
"@
$commands += $command
}
# Create URL address group
if ($urls.Count -gt 0) {
$urlGroupName = "$serviceName`_URL_Group"
$urlGroupMembers = $urls | ForEach-Object { "$serviceName`_$_" }
$urlGroupMembersString = $urlGroupMembers -join ' '
$command = @"
config firewall addrgrp
edit '$urlGroupName'
set member $urlGroupMembersString
next
end
"@
$commands += $command
}
return $commands
}
function Get-MicrosoftAddressListForFortiGate {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[ValidateSet("USGovGCCHigh", "China", "Worldwide", "USGovDoD", "Germany")]
[string]$site
)
$site = $site.ToLower()
# Path to download location
$downloadPath = "C:\temp"
$fileName = "MSAddresses-$site.json"
# Get Client Request ID
$clientRequestID = New-Guid
# Download the Microsoft IP and URL address ranges JSON file
$uri = "https://endpoints.office.com/endpoints/$site"+"?clientrequestid=$clientRequestID"
Invoke-WebRequest -Uri $uri -OutFile "$downloadPath\$fileName"
# Read the JSON file
$jsonFilePath = "$downloadPath\$fileName"
$jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json
# Initialize an array to store the FortiGate CLI commands
$fortiGateCommands = @()
# Loop through each service area in the JSON
foreach ($service in $jsonContent) {
$serviceName = $service.serviceAreaDisplayName -replace ' ', '_'
$fortiGateCommands += Create-IP-AddressObjects -serviceName $serviceName -ips $service.ips
$fortiGateCommands += Create-URL-AddressObjects -serviceName $serviceName -urls $service.urls
$fortiGateCommands += Create-AddressGroups -serviceName $serviceName -ipAddresses $service.ips -urls $service.urls
}
# Output the FortiGate CLI commands to a file
$outputFilePath = "C:\temp\"+"$site - Microsoft Address Objects.conf"
$fortiGateCommands | Out-File -FilePath $outputFilePath -Encoding ASCII
# Output the result
Write-Output "FortiGate CLI commands have been generated successfully."
Write-Output "The commands have been saved to $outputFilePath."
}