|
# Load Windows Forms |
|
Add-Type -AssemblyName System.Windows.Forms |
|
|
|
# Function to get the whitelist file path |
|
function Get-WhitelistFile { |
|
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog |
|
$openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" |
|
$openFileDialog.Title = "Select the whitelist file" |
|
|
|
if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { |
|
return $openFileDialog.FileName |
|
} else { |
|
Write-Host "No file selected. Exiting script." |
|
exit |
|
} |
|
} |
|
|
|
# Function to get the save file path for the CSV |
|
function Get-SaveFilePath { |
|
$saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog |
|
$saveFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*" |
|
$saveFileDialog.Title = "Save the results CSV file" |
|
$saveFileDialog.DefaultExt = "csv" |
|
$saveFileDialog.AddExtension = $true |
|
|
|
if ($saveFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { |
|
return $saveFileDialog.FileName |
|
} else { |
|
Write-Host "No save location selected. Results will not be saved to a file." |
|
return $null |
|
} |
|
} |
|
|
|
# Get the whitelist file path from the user |
|
$whitelistPath = Get-WhitelistFile |
|
|
|
# Read the whitelist file |
|
$websites = Get-Content $whitelistPath |
|
|
|
# Initialize results array |
|
$results = @() |
|
|
|
# Test connectivity for each website |
|
foreach ($website in $websites) { |
|
try { |
|
$pingResult = Test-Connection -ComputerName $website -Count 1 -ErrorAction Stop |
|
$status = "Reachable" |
|
$latency = $pingResult.Latency |
|
} catch { |
|
$status = "Unreachable" |
|
$latency = $null |
|
} |
|
|
|
$result = [PSCustomObject]@{ |
|
Website = $website |
|
Status = $status |
|
Latency = if ($latency) { "$latency ms" } else { "N/A" } |
|
} |
|
$results += $result |
|
|
|
# Display progress |
|
if ($latency) { |
|
Write-Host "Testing $website : $status ($latency ms)" |
|
} else { |
|
Write-Host "Testing $website : $status" |
|
} |
|
} |
|
|
|
# Display results |
|
$results | Format-Table -AutoSize |
|
|
|
# Get save file path from the user |
|
$csvPath = Get-SaveFilePath |
|
|
|
# Export results to CSV if a path was selected |
|
if ($csvPath) { |
|
try { |
|
$results | Export-Csv -Path $csvPath -NoTypeInformation -ErrorAction Stop |
|
Write-Host "Results exported to: $csvPath" |
|
} catch { |
|
Write-Host "Error saving the CSV file: $_" |
|
} |
|
} |
|
|
|
# Keep the window open |
|
Write-Host "Press any key to exit..." |
|
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |