$Port = 53
$Report = @()
$Servers = Import-CSV c:\folder\filename.csv
Foreach ($Server in $Servers) {
$Alive = Test-NetConnection -Port $port -ComputerName $Server.IPAddress | Select TcpTestSucceeded
IF ($Alive.TcpTestSucceeded -eq "True") {
$TestResult = New-Object psobject -Property @{
IPAddress = $Server.IPAddress
Result = "Port " + $Port + " is OPEN"
}
}
ELSE {
$TestResult = New-Object psobject -Prop @{
IPAddress = $Server.IPAddress
Result = "Port " + $Port + " is CLOSED"
}
}
$Report += $TestResult
}
$Report | select IPAddress, Result | Export-Csv c:\Folder\Report.csv -nti
the command would be
Open in new window