Created
October 16, 2013 00:16
-
-
Save grantcarthew/7000687 to your computer and use it in GitHub Desktop.
Determine the active IP address on a Windows machine with PowerShell.
http://uglygizmo.blogspot.com.au/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Returns IPv4 address details for the local machine. | |
Information is gathered from the active interface being used by the default route. | |
#> | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param () | |
Write-Verbose -Message ("Begin: " + $MyInvocation.MyCommand.Path) | |
<# | |
.Synopsis | |
This function uses regular expressions to return the first IPv4 | |
dotted decimal notation string in the list of strings passed. | |
#> | |
function Get-First | |
{ | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param( $List ) | |
[Regex]$reg = "\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}" | |
$result = "" | |
foreach ($ip in $List) | |
{ | |
$match = $reg.Match($ip) | |
if ($match.Success) | |
{ | |
$result = $match.Groups[0].Value | |
break | |
} | |
} | |
$result | |
} | |
Write-Verbose -Message "Getting the interface index being used by the default route." | |
$NICIndex = Get-CimInstance -ClassName Win32_IP4RouteTable | | |
Where-Object { $_.Destination -eq "0.0.0.0"-and $_.Mask -eq "0.0.0.0" } | | |
Sort-Object Metric1 | | |
Select-Object -First 1 | | |
Select-Object -ExpandProperty InterfaceIndex | |
Write-Verbose -Message "Getting the default route network adapter configuration." | |
$AdapterConfig = Get-CimInstance -ClassName Win32_NetworkAdapter | | |
Where-Object { $_.InterfaceIndex -eq $NICIndex } | | |
Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration | |
Write-Verbose -Message "Populating a custom PSObject with the desired details." | |
$ipconfig = [PSCustomObject]@{Description = $AdapterConfig.Description; | |
MACAddress = $AdapterConfig.MACAddress; | |
Address = (Get-First $AdapterConfig.IPAddress); | |
NetMask = (Get-First $AdapterConfig.IPSubnet); | |
Gateway = (Get-First $AdapterConfig.DefaultIPGateway); | |
DHCPServer = $AdapterConfig.DHCPServer; | |
DNSHostName = $AdapterConfig.DNSHostName; | |
DNSDomain = $AdapterConfig.DNSDomain; | |
DNSSearch = $AdapterConfig.DNSDomainSuffixSearchOrder} | |
# Return the result. | |
$ipconfig | |
Write-Verbose -Message ("End: " + $MyInvocation.MyCommand.Path) |
Or even more compact
(Get-NetIPAddress -InterfaceIndex (Get-NetIPInterface -ConnectionState Connected -InterfaceAlias "Ethernet*").ifIndex).IPv4Address
Hi @MichaelWerner,
That's not quite the same mate. You are making an assumption that there is only one ethernet interface in a connected state. As soon as you have multiple ethernet interfaces in a connected state, the order of interfaces is unknown.
This is why my example has the default route component. We need the workout the interface which is being used by the default route with the lowest metric.
You are correct. I assume I have only one connection. This should be correct for most laptops or workstations but not for a server.
…Sent from my iPhone
On Jan 6, 2021, at 7:26 PM, Grant Carthew ***@***.***> wrote:
***@***.*** commented on this gist.
Hi @MichaelWerner,
That's not quite the same mate. You are making an assumption that there is only one ethernet interface in a connected state. As soon as you have multiple ethernet interfaces in a connected state, the order of interfaces is unknown.
This is why my example has the default route component. We need the workout the interface which is being used by the default route with the lowest metric.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a little easier now. Drop the following into a script: