Skip to main content

Posts

Showing posts with the label Registry

Get your current IP Address with Powershell

There are lots of ways to do this, but I haven't found an elegant one that works well. Here is my approach that queries the registry: [array] $IPArray = @() $Adapters = Get-Childitem "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Adapters" foreach ($uid in $Adapters) { if ($uid.PSChildName -ne "NdisWanIp") { $Interface = "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" + $uid.PSChildName ## Write-Host "Checking: $Interface" $IPs = Get-ItemProperty $Interface $IPArray += $IPs.DhcpIPAddress } } $IPArray You can change "$IPs.DhcpIPAddress" to "$IPs.IPAddress" if you have a static IP.

List all IP addresses bound to a box with C#

RegistryKey adaptersRegKey = Registry.LocalMachine; adaptersRegKey = adaptersRegKey.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Adapters"); String[] adapterGuids = adaptersRegKey.GetSubKeyNames(); foreach (String giud in adapterGuids) { RegistryKey interfaceRegKey = Registry.LocalMachine; interfaceRegKey = interfaceRegKey.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" + giud); try { Console.WriteLine("Checking: " + giud); Object ipObject = interfaceRegKey.GetValue("IPAddress"); Array ipArray = (Array)ipObject; foreach (string ip in ipArray) { Console.WriteLine("Found IP: " + ip); } interfaceRegKey.Close(); } catch (NullReferenceException) { } } adaptersRegKey.Close();