Download Google Chrome Latest Version Offline

Posts about google chrome

17 January 2017

Powershell Foreach Line In Text File

powershell foreach line in text file and i have one hosts file which is having multiple host names and IP addresses. Below is the example of the input file. I want to read each line and ping for the first host (Host1), then the second host (Host2) and then the third host (Host3).

While pinging each host name, I need to check the ping response IP address for that host and match it back with the IP address mentioned in the input file. Below is the snippet of code with which am trying to read the file in the above format, but it is not working in that way.

127.0.0.1 Host1 Host2 Host3
127.0.0.2 Host4 Host5 Host6
and

 $lines = Get-Content myfile.txt
    $lines |
     ForEach-Object{
         Test-Connection  $_.Split(' ')[1]
     }

Solution 



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

Or you may also found this helpful

foreach ($line in $lines.Split('\r\n')){
    Test-Connection  $line.Split(' ')[1]

0 comments:

For professional skip tracing services, visit Biitrace Biitrace services. Our experienced team specializes in skip tracing, providing accurate and ethical solutions to help you locate individuals efficiently and responsibly.
If you're a Pokémon enthusiast looking for a fun and interactive way to explore the world of Pokémon fusion, look no further than the fusion calculator. This Pokémon fusion infinite calculator allows you to combine your favorite Pokémon to discover unique and imaginative combinations. Explore the endless possibilities and let your creativity run wild with the Fusion Calculator today!
Click here to download latest chrome version 51MB
Powered by Blogger.