Author Posts

August 31, 2015 at 8:06 am

Hi all,
I have a script that has a part that needs to retrieve a list of IP addresses from a linux server which runs Netapp DFM.
when I execute the command and put is in a variable I get a each character in a new line, like a here-string.
Now, I need to only take the IP addresses out and put them all in an array.
Any idea how I can do that?
The variable output looks something like that:

PS C:\Users\XXX> $x
A
B
C
D
E
1
9
2
.
1
6
8
.
1
6
.
1
0
A
B
C
D
E
1
9
2
.
1
6
8
.
1
2
.
1
2
A
S
D
F

August 31, 2015 at 8:12 am

Something like this should work:

$ips = ($x -join '' -replace '[\r\n]') |
       Select-String -Pattern '[\d\.]+' -AllMatches |
       Select-Object -ExpandProperty Matches |
       Select-Object -ExpandProperty Value

August 31, 2015 at 8:28 am

Thanks for the very prompt reply, it works great on the test $x example, I will try that also on my script.
I do have a FOR loop that does the work but I figured there must be a better and simpler way.
If anyone find that helpful here is my loop (created with a help of a friend)

$ntp=Invoke-NaSsh -Name 192.168.111.11 -Credential $Cred -Command "dfm host list"
#$ntp = "dfm host list"
$final=" "
$test = $ntp.ToCharArray()
$x = $ntp.Length
for ($i = 0; $i -lt $x; $i++)
{
if ($test[$i] -eq "1" -and $test[$i+1] -eq "9" -and $test[$i+2] -eq "2" -and $test[$i+3] -eq ".")
{
$final += "`n"
for ($ii = $i; $ii -lt $i+16; $ii++)
{
$final += $test[$ii]
}
}
}

$NetappList = $final.split(ā€œ`nā€).trim()
$NetappList=$NetappList[1..($NetappList.Length-1)]

August 31, 2015 at 8:51 am

Hi Dave,
I tried that within my script and unfortunately the regex has caught other numbers besides the IPs.
*one correction is that I get the output of the command like that:

ID Type Host Name Host Address ProductId Deleted
—- ———————— ———————————– —————————————– ———— ——–
6721 Active/Active Controller xxx-xxx-xxx.com 192.168.111.12 2545002333 No
6738 Active/Active Controller yy-y-yyyy.com 192.168.111.13 24723423444 No
718 Active/Active Controller zzz-zzzz-zzzzz.com 192.168.111.14 1523223549 No

And the output from the regex is: (just and example I changed the real numbers)

192.168.111.12
2545002333
9675
1
2
192.168.111.13
24723423444
3365
1
1.
.
192.168.111.14
1523223549
3366
1
2.

Any idea how to adjust it now?

August 31, 2015 at 9:05 am

That's a mess. I would be trying to fix the upstream bit that is giving you each character on a single line, rather than trying to put it back together when you have multiple consecutive numeric fields. Fore example, is that 192.168.111.122545002333 meant to be an IP address that ends in .1, .12, or .122? There's no way to know.

August 31, 2015 at 9:58 am

I understand the mess.
But the output of your parsing gives us something that is not that bad.
I mean the output pattern is always the same, I get the IP I want and some other numeric lines afterwards.
How can I select only the lines the start with '192.' and drop all the rest?

August 31, 2015 at 10:52 am

OK I played with it a bit and this has gave me the output I needed.

$ips = (-split $x) |
Select-String -Pattern '[\d\.]+' -AllMatches |
Select-String -Pattern "192."