How to read a line from a file in PowerShell

前端 未结 2 1145
爱一瞬间的悲伤
爱一瞬间的悲伤 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:25

    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

    0 讨论(0)
  • 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" }
        }
    }
    
    0 讨论(0)
提交回复
热议问题