Skip to content

Instantly share code, notes, and snippets.

@rezarahimian
Created October 11, 2020 00:45
Show Gist options
  • Save rezarahimian/7c2365c80eecf3851f1e00737cf9481a to your computer and use it in GitHub Desktop.
Save rezarahimian/7c2365c80eecf3851f1e00737cf9481a to your computer and use it in GitHub Desktop.
$Servers = Import-Csv -Path 'D:\Data\ListOfServers.csv'
$Output = @('Source','Destination','IPAddress','PingStatus','Date','Time','ErrorMessage')
$Servers = $Servers | Select-Object -Property @{Name='HostName';Expression={$_.ComputerName}},@{Name='Data';Expression={ [String]::Empty | Select-Object -Property $Output}}
$Servers | fl *
$Servers | ForEach-Object -Parallel {
$_.Data.Date = (Get-Date -Format 'dd-MMM-yyyy')
$_.Data.Time = (Get-Date -Format 'HH:mm:ss')
$_.Data.Source = $env:COMPUTERNAME
$_.Data.Destination = $_.HostName
$PingInfo = Test-Connection -ComputerName $_.Data.Destination -Count 1 -ErrorAction SilentlyContinue
if ($PingInfo)
{
$_.Data.IPAddress = $PingInfo.Address
$_.Data.PingStatus = $PingInfo.Status
}
else
{
$_.Data.PingStatus = 'Failed'
$_.Data.ErrorMessage = ('{0} is offline or not accessible from {1}' -f $_.Data.Destination, $_.Data.Source)
}
} -ThrottleLimit 10
$Servers.Data | Export-Csv -Path 'D:\Data\PingResult.csv' -NoTypeInformation -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment