Skip to content

Instantly share code, notes, and snippets.

@jermspeaks
Last active July 17, 2024 20:40
Show Gist options
  • Save jermspeaks/e1fd2d3eb148d9c544e82eab099995a3 to your computer and use it in GitHub Desktop.
Save jermspeaks/e1fd2d3eb148d9c544e82eab099995a3 to your computer and use it in GitHub Desktop.
PowerShell script to test network connections at customer sites to determine if the whitelist is okay.

To convert this PowerShell script into an executable (.exe) file, you can still use PS2EXE as described before:

  1. Install PS2EXE (if you haven't already):

    Install-Module -Name ps2exe
    
  2. Convert the script to an executable:

    Invoke-ps2exe .\ping_websites.ps1 .\ping_websites.exe
    

This will create an executable file named ping_websites.exe that you can run on any Windows machine. When run, it will open a file dialog for the user to select the whitelist file, then proceed with pinging the websites and generating the report.

# 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")
google.com
bing.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment