-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExport-MicrosoftEndpoints.ps1
81 lines (77 loc) · 3.46 KB
/
Export-MicrosoftEndpoints.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
Function Export-MicrosoftEndpoints {
<#
.SYNOPSIS
Retrieves and processes Microsoft Endpoints.
.DESCRIPTION
This function retrieves Microsoft Endpoints from a specific URL, processes the data, and provides various details related to service areas, URLs, IP addresses, ports, notes, express routes, categories, and requirements.
.PARAMETER CSVPath
Specifies the path to export the retrieved Microsoft Endpoints data in CSV format.
.EXAMPLE
Export-MicrosoftEndpoints
.NOTES
v0.0.1
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, ParameterSetName = "CSV")]
[string]$CSVPath
)
BEGIN {
$SiteUrl = 'https://learn.microsoft.com/en-us/microsoft-365/enterprise/urls-and-ip-address-ranges?view=o365-worldwide'
$JsonLink = ''
try {
$site = Invoke-WebRequest -Uri $SiteUrl -UseBasicParsing -ErrorAction Stop -Verbose
$jsonLink = ($site.Links | Where-Object OuterHTML -match 'JSON formatted').href
}
catch {
Write-Error -Message "Error accessing the website: $SiteUrl"
return
}
try {
$Endpoints = Invoke-WebRequest -Uri $jsonLink -ErrorAction Stop | ConvertFrom-Json
Write-Host "Downloading worldwide Microsoft Endpoints" -ForegroundColor Green
}
catch {
Write-Error -Message "Error downloading worldwide Microsoft Endpoints from $($jsonLink)"
return
}
$total = @()
}
PROCESS {
Write-Host "Processing items..." -ForegroundColor Green
foreach ($Endpoint in $Endpoints) {
$IPaddresses = if (-not $Endpoint.ips) { 'Not available' } else { $Endpoint.ips -split ' ' -join ', ' }
$TCPPorts = if (-not $Endpoint.tcpPorts) { 'Not available' } else { $Endpoint.TCPPorts -split ',' -join ', ' }
$UDPPorts = if (-not $Endpoint.udpPorts) { 'Not available' } else { $Endpoint.udpPorts -split ',' -join ', ' }
$Notes = if (-not $Endpoint.notes) { 'Not available' } else { $Endpoint.Notes }
$URLlist = if (-not $Endpoint.urls) { 'Not available' } else { $Endpoint.urls -join ', ' }
$Total += [PSCustomObject]@{
serviceArea = $Endpoint.serviceArea
serviceAreaDisplayName = $Endpoint.serviceAreaDisplayName
urls = $URLlist
ips = $IPaddresses
tcpPorts = $TCPPorts
udpPorts = $UDPPorts
notes = $Notes
expressRoute = $Endpoint.expressRoute
category = $Endpoint.Category
required = $Endpoint.required
}
}
}
END {
if ($CSVPath) {
try {
$Total | Sort-Object serviceAreaDisplayName | Export-Csv -Path $CSVPath -Encoding UTF8 -Delimiter ';' -NoTypeInformation -ErrorAction Stop
Write-Host ("Saved results to {0}`nDone!" -f $CSVPath) -ForegroundColor Green
}
catch {
Write-Warning -Message ("Could not save results to {0}" -f $CSVPath)
}
}
else {
Write-Host "Exporting results to Out-GridView`nDone!" -ForegroundColor Green
$Total | Sort-Object serviceAreaDisplayName | Out-GridView -Title 'Microsoft Endpoints Worldwide'
}
}
}