- <#
- .SYNOPSIS
- Gets and displays the IP address of a computer
- .DESCRIPTION
- This script uses Win32_NetworkAdapterConfiguration to
- obtain, then display, a system's IP addresses.
- NB: this only works on XP or later versions of Windows.
- .NOTES
- File Name : Get-IPAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- Script postesd to:
- http://www.pshscripts.blogspot.com
- MSDN Sample at:
- http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
- .EXAMPLE
- [ps] c:\foo> .\Get-IPAddress.ps1
- IP Address : 10.10.1.115
- IP Address : fe80::3953:f67b:2f1c:1323
- IP Address : 10.10.1.120
- IP Address : fe80::d8ed:afe2:2a97:a596
- 4 IP addresses found on this system
- #>
- ##
- # Start of Script
- ##
- # Get Networking Adapter Configuration
- $Computer = "."
- $IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration
- # Iterate and get IP address
- $count = 0
- foreach ($IPConfig in $IPConfigSet) {
- if ($Ipconfig.IPaddress) {
- foreach ($addr in $Ipconfig.Ipaddress) {
- "IP Address : {0}" -f $addr;
- $count++
- }
- }
- }
- if ($count -eq 0) {"No IP addresses found"}
- else {"$Count IP addresses found on this system"}
- #End of Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Thursday, 29 January 2009
Get-IPAddress.ps1
Subscribe to:
Post Comments (Atom)
6 comments:
The program is nice.
But i know my ip address through this ip-details along with router map.
SRI: what the ip-details site shows you is the external address, not yor internal address. The script shows how to get your internal address (or addresses).
If I parse out 'ipconfig' in Powershell as below I get five addresses which System.Net.IPAddress validates and can piped to ping.exe as strings. If I use 'gwmi' (as below) I am returned three addresses whose IPv6 addresses don't validate in System.Net.Ipaddress or work with ping.exe but are the identical format used in Win 7 firewall logs. What pieces of Windows Networking knowledge am I missing?
$IPAddress_ipconfig=(ipconfig | Select-string Address) -split ": " | Select-string -notmatch ". ."
foreach ($IP in $IPAddress_ipconfig) {([System.Net.IPaddress]::Parse("$IP")).IPAddressToString}
fe80::6172:6ecf:2d05:b0ae%12
192.168.0.11
fe80::cddc:ceef:b717:a5ac%11
2001:0:4137:9e76:30c3:129:3f57:fff4
fe80::30c3:129:3f57:fff4%17
Get-WmiObject Win32_NetworkAdapterConfiguration | ? {$_.IPAddress} | Select IPaddress | fc -expand CoreOnly | findstr [0-9]
fe80::cddc:ceef:b717:a5ac
192.168.0.11
fe80::6172:6ecf:2d05:b0ae
The last two addresses shown via IPConfig are for the unnel adapter Teredo Tunneling Pseudo-Interface. It appears that WMI does not return these.
Where would I add a line to also get the computer name
In line 31, I set the computer name to "." (aka local host). If you wanted to get the hostname just used the hostname.exe eg:
$cname = hostname
"This computer is named {0}" -f $cname
Hope this helps!
Post a Comment