I am new to PS scripting. I am in need of help with my code. purpose: ping a list of IP's from a .txt file and output the result in csv format. Here is my code thus far.
$Iplist = Get-Content ips.txt
$group = $()
foreach ($ip in $Iplist)
{
$status = @{ "ServerIP Name" = $ip; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection $ip -Count 4 -ea 0 | measure-Object -Property ResponseTime -Average)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$group += $serverStatus
}
$group | export-csv c:\ping\results.csv -NoTypeInformation
Test-Connection
returns a Win32_PingStatus
object.
To see what else is available on this object in PowerShell type:
$ping = Test-Connection www.google.com #or insert favorite url here
$ping | Format-List | Out-String
Test-Connection
doesn't just return a bool. You're really close but you have to assign the return value in order to calculate the average on success:
$Iplist = Get-Content ips.txt
$group = @()
foreach ($ip in $Iplist) {
$status = @{ "ServerIP Name" = $ip; "TimeStamp" = (Get-Date -f s) }
$pings = Test-Connection $ip -Count 4 -ea 0
if ($pings) {
$status["AverageResponseTime"] =
($pings | Measure-Object -Property ResponseTime -Average).Average
$status["Results"] = "Up"
}
else {
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$group += $serverStatus
}
$group | Export-Csv c:\ping\results.csv -NoTypeInformation