Isolate two values from a text file

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.








  • Edited by Koz0s Wednesday, January 29, 2014 2:47 PM
January 29th, 2014 11:50am

Hi Koz0z,

Many ways to skin this cat, here is what I chose to do based on your existing code.

Function ExtractIP($String) { 
    $IPregex=(?<CustomerName>\w+)\s+(?<class>\w+)\s+(?<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) {
        Write-Output $matches
    } 
}
 $information = (get-content Information.txt)
Foreach($line in $information) { 
    $Data = ExtractIP($line); 
    $Name = $Data.customername; 
    $Type = $Data.Class; 
    $IpCheck= Test-Connection -Computername $Name -count 1 -ErrorAction Silentlycontinue | Select -expand IPV4Address | Select -expand IPAddressToString
}

Good Luck,

Shane

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2014 10:09pm

Hi Koz0z,

Many ways to skin this cat, here is what I chose to do based on your existing code.

Function ExtractIP($String) { 
    $IPregex=(?<CustomerName>\w+)\s+(?<class>\w+)\s+(?<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) {
        Write-Output $matches
    } 
}
 $information = (get-content Information.txt)
Foreach($line in $information) { 
    $Data = ExtractIP($line); 
    $Name = $Data.customername; 
    $Type = $Data.Class; 
    $IpCheck= Test-Connection -Computername $Name -count 1 -ErrorAction Silentlycontinue | Select -expand IPV4Address | Select -expand IPAddressToString
}

Good Luck,

Shane

I tried your code, But the $string is not matching with $IPregex. The value of Data is empty, Hence Name and Type values also.
  • Edited by Koz0s Thursday, January 30, 2014 7:55 AM
January 30th, 2014 10:55am

Anybody there?
Free Windows Admin Tool Kit Click here and download it now
January 30th, 2014 1:54pm

Hi,

We can use string.split() method:

$content=get-content Information.txt

$content.split(" ")

Regards,

Yan Li

January 30th, 2014 9:59pm

Hi,

We can use string.split() method:

$content=get-content Information.txt

$content.split(" ")

Regards,

Yan Li

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2014 10:52pm

Hi,

If I read your question right, you're looking to parse an input file for the hostname (first entry on each line in your text file) and then ping that hostname. If that is in fact the case, try this:

Get-Content .\serverList.txt | Select -Skip 1 | ForEach {

    if ($_ -ne '') {

        $serverName = $_.Split(' ')[0]

        Write-Host "Checking $serverName`:"

        Test-Connection $serverName

    }

}

Here's what I used as test input, based on what you posted above:

Name Type IP

abc  elite  10.0.0.1

def  gold 10.0.0.2

ghi  silver 10.0.0.3

January 31st, 2014 1:22am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics