且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从PowerShell中的文件中读取一行

更新时间:2023-11-03 08:36:58

尝试以下方法.应该很近了.

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