Sign in to follow this  
Followers 0
erebus

WMI Network settings

5 posts in this topic

Hello all, happy new year!

I am trying to find a way to get network settings from my NIC and resetting them, all by using WMI.

What I need to do with WMI:

a) Know if DHCP is enabled on NIC

:) Get the current IP, Subnet, Gateway and DNS (at least the primary)

c) Be able to set NIC's settings to auto (DHCP) or manual (reconfig IP, Subnet, Gateway and DNS)

Anybody knows if this is possible?

I've seen a good script here showing the usage of Win32_NetworkAdapterSetting Class but it doesn't return the DNS settings and I think it can't set options on the NIC.

Also, if setting options via WMI is not possible, I still need the functionality at least to get the current settings (I can then set them via netsh, although it is very slow).

I appreciate your help.

Share this post


Link to post
Share on other sites



No ideas guys?

Share this post


Link to post
Share on other sites

In my tests WMI was not robust stable enough for my requirements. IIWM, I'd utilize netsh and possibly ipconfig with _RunReadStd() to dynamically generate / execute the necessary netsh scripts.

Let us know how it goes, w/ some code snippets. This has been one of my backburner interests.


Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

Make sure you are already on DHCP before running this, or some things will fail.

$objWMI = ObjGet("winmgmts:\\.\root\cimv2")
$collection = $objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")


For $obj In $collection
    If $obj.DHCPEnabled = -1 Then
        $DHCPStatus = "TRUE"
    Else
        $DHCPStatus = "FALSE"
    EndIf
    
    $IP = $obj.IPAddress
    $Subnet = $obj.IPSubnet
    $Gateway = $obj.DefaultIPGateway
    $DNS = $obj.DNSServerSearchOrder
    

    MsgBox(1, "", "IP Address: " & $IP[0] & @CRLF & _
            "Subnet: " & $Subnet[0] & @CRLF & _
            "Gateway: " & $Gateway[0] & @CRLF & _
            "First DNS: " & $DNS[0] & @CRLF & _
            "DHCP Enabled: " & $DHCPStatus)

Next


MsgBox(1,"","Setting new information")

$IP[0] = "10.0.0.5"
$Subnet[0] = "255.0.0.0"
$Gateway[0] = "10.0.0.1"
Redim $DNS[2]
$DNS[0] = "4.2.2.1"
$DNS[1] = "4.2.2.2"

For $obj in $collection

    $err = $obj.EnableStatic($IP,$Subnet)
    $err2 = $obj.SetGateways($Gateway)
    $err3 = $obj.SetDNSServerSearchOrder($DNS)
    
    If $err = 1 Then
        MsgBox(1,"","Unable to set static IP until reboot")
    ElseIf $err > 1 Then
        MsgBox(1,"","Unable to set IP")
    EndIf
    
    ;etc err2, err3..
    
Next

Share this post


Link to post
Share on other sites

This is really similar what I'm trying to do. I'm looking to output the current NIC settings, including IP, subnet, default gateway, DHCP server, primary DNS and WINS. Anyone have something made already that would do this? Or at least how to access that information with the intention of changing those settings.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0