Jump to content

WMI Object Syntax


Recommended Posts

I am trying to get the Primary DNS server of a workstation with my script.  I work with WMI a lot with other scripting languages, so I am no stranger to the WMI environment.  Can some let me know what I am doing wrong in this script.  This is a hybrid of what I found with Google and wrote myself.

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
For $objItem In $colItems

  $DNSServers = $objItem.DNSServerSearchOrder

  $primaryDNS = $DNSServers[0]
Next
Msgbox(0,"", $primaryDNS)

 

Link to comment
Share on other sites

Ok found a couple mistakes but I am still not getting the DNS address;

Updated Code:

$objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='True'")
For $objItem In $colItems

  $DNSServers = $objItem.DNSServerSearchOrder(0)
  $primaryDNS = $DNSServers

Next
Msgbox(0,"", $primaryDNS)

 

Link to comment
Share on other sites

The updated code returned my DNS address correctly. 

What is this code giving you?

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

No problem, happy to test. ^_^

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

The result is blank because you have more than one network adapter. Your MsgBox shows the DNS servers for the last network adapter.

Move your MsgBox line in the loop and you will see several message box with blank values.

$objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='True'")

$primaryDNS = ""

For $objItem In $colItems
    $aDNSServers = $objItem.DNSServerSearchOrder
    If IsArray($aDNSServers) Then
        $primaryDNS = $aDNSServers[0]
        ExitLoop
    EndIf
Next

MsgBox(0, "", $primaryDNS)

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...