How to read a line from a file in PowerShell

前端 未结 2 1149
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 20:30

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

2条回答
  •  借酒劲吻你
    2020-12-16 21:28

    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" }
        }
    }
    

提交回复
热议问题