Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Isolate two values from a text file

$
0
0

I have a text file Information.txt which has values like:-

Name Type IP

abc elite  10.0.0.1

def  gold 10.0.0.2

ghi  silver 10.0.0.3


$global:Content = get-content Information.txt Function ExtractIP($String) { $IPregex=‘(?<Address>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))’ If ($String -Match $IPregex) {$Matches.Address} } IPAddress

{Foreach($line in $Content){$Ip = ExtractIP($line).Trim();$Name = $line.Replace($Ip,' ').Trim();$Type = $line.Replace($Name,' ').Trim();$IpCheck=Test-Connection -Computername $Name-count 1 -ErrorAction Silentlycontinue | Select IPV4Address

}

}

IPAddress

When i run the above code, Instead of pinging the server "abc", it pings abc-elite. By using the regex expression, I can isolate the Ip address, but how do I isolate name and Type?

FOr ex:-

Name     Type    IP

-------     ------    ------

abc         elite   10.0.0.1

I want to get the IP address by using regexexpression, replace the IP address with white space, read the servername and ping it. How do I get only Name and replace Type and IP address with white spaces.









Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>