I am novice to the PowerShell scripting. I am looking to fulfill one of my requirement.
I have one hosts file which is having multiple host names and IP addresses. B
I'm never seen anything from PowerShell, but I mean it can be helpful for you. Something like the following:
foreach ($line in $lines.Split('\r\n')){
Test-Connection $line.Split(' ')[1]
}
http://en.wikipedia.org/wiki/Newline
Try the following approach. It should be close.
$lines = Get-Content myfile.txt | Where {$_ -notmatch '^\s+$'}
foreach ($line in $lines) {
$fields = $line -split '\s+'
$ip = $fields[0]
$hosts = $fields[1..3]
foreach ($h in $hosts) {
$hostIP = (Test-Connection $h -Count 1).IPV4Address.ToString()
if ($hostIP -ne $ip) { "Invalid host IP $hostIP for host $h" }
}
}