forked from DellGEOS/AzureLocalHOLs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AzSHCIURLTester.ps1
292 lines (251 loc) · 11.2 KB
/
AzSHCIURLTester.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Initialize the results array
$results = @()
# User-defined keyvault URL replacement (modify if needed)
$KeyVaultReplacement = "demo1.vault.azure.net" # Replace with your own keyvault URL if needed
# Function to extract domains from URLs
function Get-DomainFromURL {
param (
[string]$url
)
$url = $url -replace "^https?://", ""
if ($url -match ":(\d+)$") {
$port = [int]($url -replace ".*:(\d+)$", '$1')
$url = $url -replace ":(\d+)$", ""
} else {
$port = $null
}
$domain = $url -split '/' | Select-Object -First 1
return @{ Domain = $domain; Port = $port }
}
# Function to test connectivity
function Test-Connectivity {
param (
[string]$url,
[int]$port
)
$testResult = Test-NetConnection -ComputerName $url -Port $port -WarningAction SilentlyContinue
$status = if ($testResult.TcpTestSucceeded) { "Success" } else { "Failed" }
$ipAddress = $testResult.RemoteAddress
return $status, $ipAddress
}
# Function to perform NTP test for time.windows.com
function Test-NTPConnectivity {
param (
[string]$ntpServer
)
$ntpResult = w32tm /stripchart /computer:$ntpServer /dataonly /samples:1
if ($ntpResult -match "error:") {
$status = "Failed"
$ipAddress = ""
} else {
$status = "Success"
if ($ntpResult -match "\[(.*?)\]") {
$ipAddress = $matches[1]
} else {
$ipAddress = ""
}
}
return $status, $ipAddress
}
# Enhanced function to expand wildcard URLs dynamically
function Expand-WildcardUrlsDynamically {
param (
[array]$results
)
# Split into wildcard and non-wildcard results
$wildcardResults = $results | Where-Object { $_.IsWildcard -eq $true }
$nonWildcardResults = $results | Where-Object { $_.IsWildcard -eq $false }
$expandedResults = @()
foreach ($wildcard in $wildcardResults) {
# Create a regex pattern from the wildcard URL
$wildcardPattern = $wildcard.URL -replace "\*", ".*"
# Find matching non-wildcard URLs
$matchingUrls = $nonWildcardResults | Where-Object { $_.URL -match "^$wildcardPattern$" }
if ($matchingUrls.Count -eq 0) {
# No matches found, retain the original wildcard entry
$expandedResults += $wildcard
} else {
foreach ($match in $matchingUrls) {
# Handle both HTTP and HTTPS formats
$newEntryHttp = $wildcard.PSObject.Copy()
$newEntryHttp.URL = $match.URL
$newEntryHttp.Port = 80
$newEntryHttp.Note = "Expanded Wildcard URL"
$newEntryHttp.IsWildcard = $false
$newEntryHttp.Status, $newEntryHttp.IPAddress = Test-Connectivity -url $match.URL -port 80
$expandedResults += $newEntryHttp
$newEntryHttps = $wildcard.PSObject.Copy()
$newEntryHttps.URL = $match.URL
$newEntryHttps.Port = 443
$newEntryHttps.Note = "Expanded Wildcard URL"
$newEntryHttps.IsWildcard = $false
$newEntryHttps.Status, $newEntryHttps.IPAddress = Test-Connectivity -url $match.URL -port 443
$expandedResults += $newEntryHttps
}
}
}
$expandedResults += $nonWildcardResults
return $expandedResults
}
# Function to manually test known subdomains for wildcard URLs
function Test-ManuallyDefinedSubdomains {
param (
[array]$wildcardUrls
)
# Define manually known subdomains to test for each wildcard
$manualSubdomains = @(
@{ Wildcard = "*.blob.storage.azure.net"; Subdomains = @("mystorageaccount.blob.core.windows.net") },
@{ Wildcard = "*.download.windowsupdate.com"; Subdomains = @("download.windowsupdate.com", "fe2.update.microsoft.com") },
@{ Wildcard = "*.endpoint.security.microsoft.com"; Subdomains = @("global.endpoint.security.microsoft.com") },
@{ Wildcard = "*.prod.hot.ingest.monitor.core.windows.net"; Subdomains = @("eastus.prod.hot.ingest.monitor.core.windows.net") },
@{ Wildcard = "*.windowsupdate.microsoft.com"; Subdomains = @("update.microsoft.com") }
)
$manualResults = @()
# Test each subdomain for connectivity
foreach ($entry in $manualSubdomains) {
$wildcard = $entry.Wildcard
$subdomains = $entry.Subdomains
foreach ($subdomain in $subdomains) {
foreach ($port in @(80, 443)) {
$status, $ipAddress = Test-Connectivity -url $subdomain -port $port
$manualResults += [PSCustomObject]@{
RowID = 0
URL = $subdomain
Port = $port
IsWildcard = $false
Note = "Manually defined URL"
Status = $status
IPAddress = $ipAddress
}
}
}
}
return $manualResults
}
# Function to process results
function Process-Results {
param (
[array]$results
)
# Remove duplicate URLs with the same port
$results = $results | Sort-Object URL, Port -Unique
# Assign Row IDs and reorder columns
$rowIdCounter = 1
foreach ($result in $results) {
$result.RowID = $rowIdCounter
$rowIdCounter++
}
$results = $results | Select-Object RowID, URL, Port, IsWildcard, Note, Status, IPAddress
# Export results to CSV
$csvFile = "ConnectivityTestResults.csv"
$results | Export-Csv -Path $csvFile -NoTypeInformation
Write-Host "Test results have been saved to $csvFile"
# Display failed and skipped URLs
$failedResults = $results | Where-Object { $_.Status -eq "Failed" }
$skippedResults = $results | Where-Object { $_.Status -like "Skipped*" }
if ($failedResults.Count -gt 0) {
Write-Host "The following URLs failed:"
$failedResults | Format-Table -Property RowID, URL, Port, Status -AutoSize
} else {
Write-Host "No URLs failed."
}
if ($skippedResults.Count -gt 0) {
Write-Host "The following URLs were skipped:"
$skippedResults | Format-Table -Property RowID, URL, Status -AutoSize
} else {
Write-Host "No URLs were skipped."
}
}
# Load URLs from environment checker (Targets.json)
$Location = (Get-Module -Name AzStackHci.EnvironmentChecker -ListAvailable).ModuleBase
$Files = Get-ChildItem -Recurse -Path $Location | Where-Object Name -like "*Targets.json"
foreach ($File in $Files) {
$content = Get-Content -Path $File.FullName
$object = $content | ConvertFrom-Json
foreach ($item in $Object) {
foreach ($endpoint in $Item.Endpoint) {
$domainPort = Get-DomainFromURL -url $endpoint
$url = $domainPort.Domain
$port = if ($domainPort.Port) { $domainPort.Port } else { if ($item.Protocol -eq 'https') { 443 } else { 80 } }
$results += [PSCustomObject]@{
RowID = 0
URL = $url
Port = $port
IsWildcard = $false
Note = "Environment Checker URL"
Status = ""
IPAddress = ""
}
}
}
}
# Load URLs from GitHub pages
$regionUrls = @{
"East US" = "https://raw.githubusercontent.com/Azure/AzureStack-Tools/master/HCI/EastUSendpoints/eastus-hci-endpoints.md"
"West Europe" = "https://raw.githubusercontent.com/Azure/AzureStack-Tools/master/HCI/WestEuropeendpoints/westeurope-hci-endpoints.md"
"Australia East" = "https://raw.githubusercontent.com/Azure/AzureStack-Tools/master/HCI/AustraliaEastendpoints/AustraliaEast-hci-endpoints.md"
"Canada Central" = "https://raw.githubusercontent.com/Azure/AzureStack-Tools/master/HCI/CanadaCentralEndpoints/canadacentral-hci-endpoints.md"
"Central India" = "https://raw.githubusercontent.com/Azure/AzureStack-Tools/refs/heads/master/HCI/IndiaCentralEndpoints/IndiaCentral-hci-endpoints.md"
}
# Download and parse URLs from GitHub pages
$region = Read-Host "Select a region (East US, West Europe, Australia East, Canada Central, Central India)"
if ($regionUrls.ContainsKey($region)) {
$endpointUrl = $regionUrls[$region]
$endpointsContent = Invoke-WebRequest -Uri $endpointUrl -UseBasicParsing
$lines = $endpointsContent.Content -split "`n"
foreach ($line in $lines) {
if ($line -match "^\|\s*(\d+)\s*\|") {
$rowId = [int]($line -replace "^\|\s*(\d+)\s*\|.*", '$1')
$columns = $line -split "\|"
$url = $columns[3].Trim()
$ports = $columns[4].Trim() -split ','
$url = (Get-DomainFromURL -url $url).Domain
foreach ($port in $ports) {
$isWildcard = $url.Contains("*")
$results += [PSCustomObject]@{
RowID = 0
URL = $url
Port = [int]$port
IsWildcard = $isWildcard
Note = if ($isWildcard) { "Wildcard URL" } else { "GitHub URL" }
Status = ""
IPAddress = ""
}
}
}
}
}
# Add Dell URLs
$additionalUrls = @(
[PSCustomObject]@{ RowID = 0; URL = "downloads.emc.com"; Port = 443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" },
[PSCustomObject]@{ RowID = 0; URL = "dl.dell.com"; Port = 443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" },
[PSCustomObject]@{ RowID = 0; URL = "esrs3-core.emc.com"; Port = 443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" },
[PSCustomObject]@{ RowID = 0; URL = "esrs3-core.emc.com"; Port = 8443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" },
[PSCustomObject]@{ RowID = 0; URL = "esrs3-coredr.emc.com"; Port = 443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" },
[PSCustomObject]@{ RowID = 0; URL = "esrs3-coredr.emc.com"; Port = 8443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" },
[PSCustomObject]@{ RowID = 0; URL = "colu.dell.com"; Port = 443; IsWildcard = $false; Note = "Dell URL"; Status = ""; IPAddress = "" }
)
$results += $additionalUrls
# Process URLs
$skipUrls = @("*.waconazure.com", "wustat.windows.com", "<yourarcgatewayendpointid>.gw.arc.azure.net")
foreach ($urlObj in $results) {
if ($urlObj.URL -eq "time.windows.com") {
$urlObj.Status, $urlObj.IPAddress = Test-NTPConnectivity -ntpServer $urlObj.URL
} elseif ($urlObj.URL -eq "yourhcikeyvaultname.vault.azure.net") {
$urlObj.URL = $KeyVaultReplacement
$urlObj.Status, $urlObj.IPAddress = Test-Connectivity -url $urlObj.URL -port $urlObj.Port
} elseif ($skipUrls -contains $urlObj.URL) {
$urlObj.Status = "Skipped: Known Incorrect URL"
} elseif ($urlObj.Note -eq "Wildcard URL") {
continue
} else {
$urlObj.Status, $urlObj.IPAddress = Test-Connectivity -url $urlObj.URL -port $urlObj.Port
}
}
# Dynamically expand wildcard URLs
$allUrlsToTest = Expand-WildcardUrlsDynamically -results $results
# Test manually defined subdomains
$manualSubdomainTests = Test-ManuallyDefinedSubdomains -wildcardUrls $results
$allUrlsToTest += $manualSubdomainTests
# Process the final results
Process-Results -results $allUrlsToTest