With PowerShell 2.0 if I run the following on a Windows 7 machine:
(Get-WmiObject Win32_NetworkAdapterConfiguration |
Where { $_.IPAddress }
it returns
192.168.83.26
fe80::<IPv6 address>
If I run:
(Get-WmiObject Win32_NetworkAdapterConfiguration |
Where { $_.IPAddress }.ToString()
It returns
System.Object[]
Why? And how can I resolve it?
FYI, I’m actually trying to use .ToString().split('.')[0..2] -join '.' but I’m pretty sure it’s the .ToString that’s causing the issue
function Get-IPAddress{
(Get-WmiObject Win32_NetworkAdapterConfiguration |
Where { $_.IPAddress } |
Select -Expand IPAddress).split('.')[0..2] -join '.'
}
Get-IPAddress
returns 192.168.10 on PoSh 3. This doesn’t seem to work on PoSh 2, and returns the `System.Object[]’ that I stated above. The error complains about not having a Method named trim. Others have suggested declaring it as a String, but I’ve been unable to make it work
Try this:
This:
returns always an
array of stringsalso in the case of a single ip address.