Powershell: Ping Stuff
From Rabbi Blog
Provide a list of IP addresses, run the script. Checks DNS and outputs a results file.
Code
$deviceNames = Get-Content "devices.txt"
$results = @()
foreach ($name in $deviceNames) {
$status = "Unknown"
try {
# Attempt to resolve DNS
[System.Net.Dns]::GetHostEntry($name)
$status = "Resolving..."
# Check if device pings (up to 4 attempts with 1 second timeout)
if (Test-Connection -ComputerName $name -Count 1 -Quiet) {
$status = "Up"
} else {
$status = "Down"
}
} catch {
# Handle DNS resolution failure
$status = "NO DNS"
}
# Create a custom object for each device
$result = New-Object PSObject -Property @{
Name = $name
Status = $status
}
# Add object to results array
$results += $result
}
# Export results to CSV
$results | Export-Csv -Path "results.csv" -NoTypeInformation -Force
Write-Host "Results exported to 'results.csv'"