Get-WmiObject Win32_NetworkAdapterConfiguration -Match failing - powershell

So I am using this:
$IPA = (Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName").IPv4Address
and then I want to use the following to grab the Subnet Mask for that IP:
$IPInfo = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object IPAddress -Match $IPA)
Followed by:
$SubMask = $IPInfo.IPSubnet[0]
But this fails with $IPInfo being blank. If I hard code the IPAddress it works:
$IPInfo = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object IPAddress -Match 10.45.22.100)
But the port IP will always be different. Why is it not taking the $IPA as a match parameter?
EDIT: The IP Address being reported has both the IPv4 and IPv6 which is why I am trying to do -Match against the IPv4.

As we have discovered in comments you are getting an array returned for your $IPA. It looks fine in console as PowerShell unrolls the array to display all elements. Since there is only one it was misleading.
((Get-NetIPAddress | Where-Object InterfaceAlias -eq "Local Area Connection").IPv4Address).gettype().fullname
System.Object[]
Likely it was trying to match "System.Object[]" which is why you did not get the result you wanted.
Few ways around this. A simple one would be to always return the -First result in your query.
(Get-NetIPAddress | Where-Object InterfaceAlias -eq "Local Area Connection").IPv4Address | select -first 1).gettype().fullname
So just use | select -first 1 and you should get the results you expect.
I caution the use of -match here. Understand that -match and -replace support regex pattern strings. So if you have regex meta characters in your strings you can get unexpected results.

This happens because $IPA is really an array of objects.
When you run Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName" it returns an array of CIM instances of type MSFT_NetIPAddress.
When you ask for IPv4Address member by running $IPA = (Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName").IPv4Address the array is still there but the elements of the array that don't have IPv4Address are not shown.
Take a look at the following example.
$NetIPAddresses = (Get-NetIPAddress | where {$_.InterfaceAlias -eq "Ethernet" -and $_.AddressFamily -eq "IPv4"})
foreach ($NetIPAddress in $NetIPAddresses) {
$IPA = $NetIPAddress.IPv4Address
$IPInfo = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object IPAddress -Match $IPA)
$IPInfo.IPSubnet[0]
}
This will show subnet mask for each IPv4 address on the given interface.

Related

how to get specific network adapter details with get-netipaddress and where-object

I am trying to get only IPv4 address details of one of my ethernet adapters. I need to filter the result with where-object. But whenever I try to add multiple parameters to where, I receive an error.
Get-NetIpAddress | where { $_.Interfaceindex -EQ 2 -and $_.AddressFamily -EQ IPv4 }
This command shows this error.
I can get the same result by just typing:
Get-NetIPAddress -InterfaceIndex 2 -AddressFamily IPv4
But I need to get the result by using where-object. Is there any way to do so? I am just learning powershell.
just add quote to string ipv4
Get-NetIpAddress | where { $_.Interfaceindex -EQ 2 -and $_.AddressFamily -EQ "IPv4" }
Try this, I've added in quotes as you had missed them on your one. I've also added in brackets, to split up the -and it's a bit more visually appealing to see what you're doing to each segment of the code.
Get-NetIpAddress | Where-Object {($_.Interfaceindex -eq "2") -and ($_.AddressFamily -eq "IPv4")}

How to get the property name set to false value

I am still new to powershell. I have been trying to get only the Nic properties that have been set to false.
So when I run:
Get-NetAdapter | fl * it brings up all the results for each property of the Nic. I only want to see the properties that have been set to false.
I have tried Get-NetAdapter | where {$_ -like $false} and Get-NetAdapter | where {$_ -like "*false*"}. Have also tried sorting but also cant get working
Much Appreciated
(Get-NetAdapter).PSObject.Properties | Where-Object Value -eq $false | Select-Object Name
If you want the results as string array instead of an object array, use Select-Object -ExpandProperty Name
Note: This only works as intended if you have a single NetAdapter. The code in the comment from PetSerAl will give you all properties from all adapters, but you don't see from which adapter.
If you want to test multiple adapters, you should write an additional loop for example like this
$adapters = Get-NetAdapter
foreach ($adapter in $adapters)
{
Write-Host -ForegroundColor Green "$($adapter.Name)"
$adapter.PSObject.Properties | Where-Object Value -eq $false | Select-Object -ExpandProperty Name
Write-Host ""
}

Want to put IP address v4 of my specifically named adapter into a variable using Powershell

I have several network adapters on my PC.
I want to simply get the IP Address v4 (with no headers or extras) of the adapter called a specific name and store it into a variable for further use.
I am able to return the IP Address with headers but not what I want.
Please Help - thanks.
Here is what I tried:
$ipa = Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } |
Select-Object { $_.IPAddress }
Edit:
$ipa = Get-NetIPAddress | where {$_.InterfaceAlias -eq "LAN2" -and
$_.AddressFamily -eq "IPv4"} | select -expandproperty ipaddress
The above code is returning both my wired adaptors' addresses but it is returning just the Ip address at least and nothing else (thanks Anthony)
There is ONLY one called "LAN2" - i only need that one - so still stuck
Update :
Austin Frenchs second solution works great - will test Owls today later - thanks everyone so far - great helpful community
simple one liner
(Get-NetIPConfiguration).ipv4address | where {$_.InterfaceAlias -eq "Lan2"} | foreach { write-host $_.ipaddress}
get from enabled network
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | select Ipaddress
only from particular adapter and ipv4
Get-WMIObject win32_NetworkAdapterConfiguration |
Where-Object { $_.IPEnabled -eq $true } |
Foreach-Object { $_.IPAddress } |
Foreach-Object { [IPAddress]$_ } |
Where-Object { $_.AddressFamily -eq 'LAN2' } |
Foreach-Object { $_.IPAddressToString }
Without access to get-netipaddress, here is a very dirty and long one liner. Change out "Local Area Connection" for your network's name.
((([System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ? {$_.Name -eq "Local Area Connection"}).GetIPProperties() | Select-Object -ExpandProperty 'UniCastAddresses' | ? {$_.IPv4Mask -ne
"0.0.0.0"}) | Select Address).Address.IPAddressToString
With Get-netIpAddress, I suspect this would also work:
$ipa = (Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } | `
Select-Object { $_.IPAddress }).IPAddress
You can accomplish what you are looking for by first getting a list of all network adapters on the machine using get-wmiobject, selecting the name you want, then piping it to get-netipaddress and then selecting the IPAddress property as shown below.
(Get-WmiObject win32_networkadapter | Where-Object {$_.name -eq "YOUR_NAME"} | Get-NetIPAddress).IPAddress

Listing the IP addresses of a specific adapter only

I am very new to powershell and trying to write a script where I BIND my Websites and FTP sites on a server to the Ip addresses present on the specific adapter of a multihome server.
The server has two adapters: Primary and Secondary
I am using the following command:
$AvailableIPs = #( Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $Null } | Select-Object -ExpandProperty IPAddress )
and this gets me all the IP addresses on the machine from primary as well as secondary.
I need to get the list if IP address only from the secondary adapter.
The situation that the Name of the secondary adapter will not be constant but the name of primary adapter will be constant so I would need to do a -ve match on the name of primary adapter.
How can I put two -ve matches in the same command?
Something like this does not work:
$AvailableIPs = #( Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $Null } | Where-Object { $_.Description -ne "<Primary Adapter Name>"} | Select-Object -ExpandProperty IPAddress )
What would be the syntax for getting the list from
Win32_NetworkAdapterConfiguration Class which is not Null and which does not have the description of "Some name"?
Change the "sometext" to whatever you want...
$AvailableIPs = Get-WmiObject Win32_NetworkAdapterConfiguration |
? {$_.IPEnabled -and $_.Description -notmatch "sometext"} |
Select -ExpandProperty IPAddress
You don't need to use more then one Where-Object (? Alias) Clause, you can join them like in the above example,
Also you you can change the operator from -notmatch to -ne (Not Equal) if you have constant specific name

PowerShell: Format Get-WmiObject output to return only the IP address

I would like to use Get-WmiObject Win32_NetworkAdapterConfiguration to return the ip address of a network card. Unfortunately, I cannot figure out how to format the output to display only the IPv.4 address.
Get-WmiObject Win32_NetworkAdapterConfiguration | Select IPAddress | Where-Object {$_.IPaddress -like "192.168*"}
Displays:
IPAddress
---------
{192.168.56.1, fe80::8980:15f4:e2f4:aeca}
Using the above output as an example, I would like it to only return 192.168.56.1 (Some clients have multiple NIC's, hence the "Where-Object")
The IPAddress property is a string[], so the following should do it:
gwmi Win32_NetworkAdapterConfiguration |
Where { $_.IPAddress } | # filter the objects where an address actually exists
Select -Expand IPAddress | # retrieve only the property *value*
Where { $_ -like '192.168.*' }
Adding a quicker answer (avoiding Where-Object and using -like operation on a list):
#(#(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -like "*.*")[0]
Hope this Helps
Thought I would share my own variation on the above, in case it helps someone. Just one line:
Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1
Cheers.
(Get-WmiObject Win32_NetworkAdapterConfiguration | where { (($_.IPEnabled -ne $null) -and ($_.DefaultIPGateway -ne $null)) } | select IPAddress -First 1).IPAddress[0]
Returns the IP-address of network connection with default gateway.
This is exactly what you need in most cases :)
Compatible with Powershell 2.0 (Windows XP) and newer.
(Get-WmiObject win32_Networkadapterconfiguration | Where-Object{$_.ipaddress -notlike $null}).IPaddress | Select-Object -First 1
Hope that this will help !
(Get-WMIObject -Class Win32_NetworkAdapterConfiguration).IPAddress