Reputation: 15
I need to get a String which is under a Specific string.
$string = 'Wireless LAN adapter Local Area Connection* 13'
ipconfig | ForEach-Object{if($_ -match $string){Select-String -AllMatches 'IPv4 Address' | Out-File C:\Temp\Avi\found.txt}}
For example, I need to get the IPv4 address under Wireless LAN adapter Local Area Connection* 13.
Wireless LAN adapter Wi-Fi: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::34f2:d41c:3889:452e%21 IPv4 Address. . . . . . . . . . . : 172.20.10.2 Subnet Mask . . . . . . . . . . . : 255.255.255.240 Default Gateway . . . . . . . . . : 172.20.10.1 Wireless LAN adapter Local Area Connection* 13: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::b946:1464:9876:9e03%29 IPv4 Address. . . . . . . . . . . : 192.168.137.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . :
Upvotes: 0
Views: 145
Reputation: 1244
There is a way to combine strings into one and then split it using a regular expression.
$s = "Wireless LAN adapter Local Area Connection* 13"
$k = "IPv4 Address"
$part = (ipconfig) -join "`n" -split "(?<=\n)(?=\S)" | Where-Object { $_.StartsWith($s) }
$part.Split("`n") |
Where-Object { $_.TrimStart().StartsWith($k) } |
ForEach-Object { $_.Split(":", 2)[1].Trim() }
Upvotes: 0
Reputation: 4081
If you are bound and determine to parse this as text you can use a regex to do so.
$a = ipconfig | Select-String 'IPv4.*\s(?<ip>(?:[0-9]{1,3}\.){3}[0-9]{1,3})'
$a.matches[0].groups["ip"].value
10.11.12.13
This uses the regex matching of Select-String to find the matches as named groups, save them as a matchinfo object, then output to screen. Regex details can be found here.
Upvotes: 0
Reputation: 1179
Like Lee alludes to, you really don't want to use ipconfig for this, it's much easier to work with the Powershell native commands. Eg. to get the IPv4 addresses for the interfaces "Ethernet 8" and "Ethernet 10" you could use something like this:
$NetworkInterfaces = @(
"Ethernet 10"
"Ethernet 8"
)
foreach ($Interface in $NetworkInterfaces) {
Get-NetIPAddress -InterfaceAlias $Interface -AddressFamily IPv4 |
Select-Object InterfaceAlias,IPAddress
}
which in my case returns this:
InterfaceAlias IPAddress
-------------- ---------
Ethernet 10 169.254.157.233
Ethernet 8 169.254.10.64
Upvotes: 1