I had a VBScript that could do this but didn't have anything in PoSh. I decided the best way to do this was to create a custom object to easily get to the octet I needed. Doing it with a PS object provides the ability to select which octet you wanted back. Here is the function.
1: function get-ip {
2: param ([string]$computer = '.')
3: if ($computer -eq '.') {$computer = $env:computername}
4:
5: # Create custom object
6: $objIP = New-Object -typeName 'psobject'
7:
8: # Connect to WMI and retrieve IP from NetworkAdapterConfiguration Class
9: [string]$IP = (gwmi win32_networkadapterconfiguration -computerName $computer | ? { $_.IPEnabled -eq $true }).IPAddress
10:
11: # Split on the '.' to get each octet
12: $octets = $IP.split(".")
13:
14: # Add octets,ip and computername to the custom object
15: Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "1stOct" -value $octets[0]
16: Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "2ndOct" -value $octets[1]
17: Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "3rdOct" -value $octets[2]
18: Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "4thOct" -value $octets[3]
19: Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "FullIPv4" -value $ip
20: Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "ComputerName" -value $computer
21:
22: return $objIP
23: }
So now you can get whatever octet by using select-object. The function can take an argument named Computer so you can retrieve remote computer addresses/octets as well.
Sjdelatorre@JDELATORRE {~} get-ip
1stOct : 192
2ndOct : 168
3rdOct : 251
4thOct : 46
FullIPv4 : 192.168.251.46
ComputerName : JDELATORRE
Sjdelatorre@JDELATORRE {~} get-ip | select 3rdoct
3rdOct
------
251
Sjdelatorre@JDELATORRE {~} get-ip | select 3rdoct,fullipv4,computername
3rdOct FullIPv4 ComputerName
------ -------- ------------
251 192.168.251.46 JDELATORRE
Sjdelatorre@JDELATORRE {~} get-ip -computer jdelatorreVPC | select 3rdoct,fullipv4,computername
3rdOct FullIPv4 ComputerName
------ -------- ------------
251 192.168.251.67 jdelatorreVPC
Sjdelatorre@JDELATORRE {~}
No comments:
Post a Comment