Ping a list of IPs and return a list of alive IPs, a list of dead IPs, and a csv of both.
$IPlist = ".\ip-list.txt" $collection = $() Clear-Content -Path ".\IP-Alive-List.txt" Clear-Content -Path ".\IP-DEAD-List.txt" Clear-Content -Path ".\IP-DEAD-or-ALIVE-List.csv" (Get-Content $IPlist) -notmatch '^#' | foreach { if (Test-Connection $_ -Count 1 -ea 0 -quiet) { $status = @{ "Alive-IPs" = $_ } Write-Host "$_ is up! Writing to IP-Alive-List.txt..." -ForegroundColor Green $_ | Out-File -FilePath .\IP-Alive-List.txt -Append "$_`tAlive" | Out-File -FilePath .\IP-DEAD-or-ALIVE-List.csv -Append } else { Write-Host "$_ is down! NOT writing to IP-Alive-List.txt" -ForegroundColor Red $_ | Out-File -FilePath .\IP-DEAD-List.txt -Append "$_`tDEAD" | Out-File -FilePath .\IP-DEAD-or-ALIVE-List.csv -Append } } Invoke-Item .\IP-Alive-List.txt
Here’s one to import a CSV with Hostnames in column A and IPs in column B:
$DefaultValue = "../../SessionList.csv" $IPlist = Read-Host "Enter CSV Path [../../SessionList.csv]" if($IPlist -eq $null){ $IPlist = $DefaultValue } if($IPlist -eq ""){ $IPlist = $DefaultValue } $IPlistData = Import-Csv $IPlist -Header HOSTNAME,IP if (!(Test-Path ".\IP-DEAD-or-ALIVE-List.csv")) { New-Item -name "IP-Alive-List.txt" -type "file" -Force New-Item -name "IP-DEAD-List.txt" -type "file" -Force New-Item -name "IP-DEAD-or-ALIVE-List.csv" -type "file" -Force } else { Clear-Content -Path ".\IP-Alive-List.txt" -Force Clear-Content -Path ".\IP-DEAD-List.txt" -Force Clear-Content -Path ".\IP-DEAD-or-ALIVE-List.csv" -Force } ForEach ($Item in $IPlistData) { if($Item.IP) { if (Test-Connection $Item.IP -Count 1 -ea 0 -quiet) { Write-Host $Item.IP - $Item.HOSTNAME "is up! Writing to IP-Alive-List.txt..." -ForegroundColor Green $Item.IP | Out-File -FilePath .\IP-Alive-List.txt -Append -Force $ItemString = $Item.IP $ItemHostnameString = $Item.HOSTNAME "Alive`t$ItemString`t$ItemHostnameString" | Out-File -FilePath .\IP-DEAD-or-ALIVE-List.csv -Append -Force } else { Write-Host $Item.IP - $Item.HOSTNAME "is down! NOT writing to IP-Alive-List.txt" -ForegroundColor Red $Item.IP | Out-File -FilePath .\IP-DEAD-List.txt -Append -Force $ItemString = $Item.IP $ItemHostnameString = $Item.HOSTNAME "DEAD`t$ItemString`t$ItemHostnameString" | Out-File -FilePath .\IP-DEAD-or-ALIVE-List.csv -Append -Force } } } Invoke-Item .\IP-Alive-List.txt Invoke-Item .\IP-DEAD-List.txt Invoke-Item .\IP-DEAD-or-ALIVE-List.csv