Mike-Ward.Net

Determining the local IP address via script

Why determining the local IP address should be such a monumental task in Windows is anyone’s guess. Here’s some JavaScript code I cobbled together from a couple of different sources for an installer script I’m writing.

function GetIpAddress()
{
    var ip = "";
    wmi = GetObject("winmgmts:!\\\\.\\root\\cimv2")
    nac = wmi.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

    for (nac = new Enumerator(nac); !nac.atEnd(); nac.moveNext())
        ip += nac.item().ipAddress(0);

    if (ip == "")
        throw "IP address not found";
        
    return ip;
}

It works but isn’t pretty. Surely there must be an easier way? (I’m not running in a browser so no Remote_Server header is available).

Signed: Irritated

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
Tweetz 1.1
More... (1075)

Donate with PayPal