Jump to content
monarch684

WMI Object Syntax

Recommended Posts

monarch684

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)

 

Share this post


Link to post
Share on other sites
monarch684

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)

 

Share this post


Link to post
Share on other sites
MikahS

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

 

Share this post


Link to post
Share on other sites
monarch684

I get nothing.  Just a blank message box.

Share this post


Link to post
Share on other sites
monarch684

Wait......tried on a different box and it worked great.  Must just be my box.  Thx.

Share this post


Link to post
Share on other sites
MikahS

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

 

Share this post


Link to post
Share on other sites
jguinch

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)

 

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

×