monarch684 Posted May 6, 2015 Posted May 6, 2015 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)
monarch684 Posted May 6, 2015 Author Posted May 6, 2015 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)
MikahS Posted May 6, 2015 Posted May 6, 2015 The updated code returned my DNS address correctly. What is this code giving you? Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy 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
monarch684 Posted May 6, 2015 Author Posted May 6, 2015 Wait......tried on a different box and it worked great. Must just be my box. Thx.
MikahS Posted May 6, 2015 Posted May 6, 2015 No problem, happy to test. Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy 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
jguinch Posted May 6, 2015 Posted May 6, 2015 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) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now