Jump to content
Dwalfware

WMI Error on second object (DNS)

Recommended Posts

Hi Folks

Can not get my head around this one.

This function gets the DNS object s

If you have two DNS entries fine. but if you have only one! Trouble

$objItem.DNSServerSearchOrder[0] = Primary DNS

$objItem.DNSServerSearchOrder[1] = Secondry DNS not always there. I need a way to return 0 if not there or the IP if its there.

Or is there a way to tell how many objects there are in the "[?]"

I'm terrible at WMI errors, thanks for help in advance.

Func _GETcurrentNICinfo2()
$oErrors = ObjEvent("AutoIt.Error", "Error_Handle")
Local $DnsSecondry
Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" )
Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
If @error Then Return 0
If NOT IsObj($query) Then
Return 0
Else
For $objItem In $query
Return $objItem.DNSServerSearchOrder[1]
next
Endif
EndFunc

 

Share this post


Link to post
Share on other sites

Maybe like this (can't test at the moment):

Func _GETcurrentNICinfo2()
    $oErrors = ObjEvent("AutoIt.Error", "Error_Handle")
    Local $DnsSecondry
    Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" )
    Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
    If @error Or NOT IsObj($query) Then Return 0
    For $objItem In $query
        $aDNSIPAddress = $objItem.DNSServerSearchOrder
        MsgBox(0, "DNSServerSearchOrder", "# of IP-Addresses: " & UBound($aDNSIPAddress))
        Return $objItem.DNSServerSearchOrder[1]
    next
EndFunc

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Taks Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
9 hours ago, water said:

Maybe like this (can't test at the moment):

Func _GETcurrentNICinfo2()
    $oErrors = ObjEvent("AutoIt.Error", "Error_Handle")
    Local $DnsSecondry
    Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" )
    Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
    If @error Or NOT IsObj($query) Then Return 0
    For $objItem In $query
        $aDNSIPAddress = $objItem.DNSServerSearchOrder
        MsgBox(0, "DNSServerSearchOrder", "# of IP-Addresses: " & UBound($aDNSIPAddress))
        Return $objItem.DNSServerSearchOrder[1]
    next
EndFunc

 

Brilliant

Thank you so much, this is exactly what I was hoping for.

D

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Taks Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
9 hours ago, water said:

:)

This worked for me, instead of porting the result, I just pulled it directly into my array I built.

Cheers for the help. 

;------------------------------------------------------------------------------------------------------------
; ------------------------
; TEST THE Primary and Secondry DNS SERVER
; ------------------------

Func _GETcurrentNICinfo2()  ; GET THE DNS SETTINGS
    $oErrors = ObjEvent("AutoIt.Error", "Error_Handle")
    Local $DnsSecondry
    Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" )
    Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
If @error Or NOT IsObj($query) Then  ; If Error or Nothing set
$AdaptorInfo[0][7] = ""
$AdaptorInfo[0][8] = ""
Endif
;....................................
If IsObj($query) Then
For $objItem In $query
;....................................
If UBound($objItem.DNSServerSearchOrder) = 1 then                ; If only one DNS is set
$String1 = "[" & UBound($objItem.DNSServerSearchOrder) -1 & "]"
$AdaptorInfo[0][7] = $objItem.DNSServerSearchOrder[$String1]
Else
$AdaptorInfo[0][7] = ""
Endif
;....................................
If UBound($objItem.DNSServerSearchOrder) = 2 then                 ; If Two DNS is set
$String1 = "[" & UBound($objItem.DNSServerSearchOrder) -2 & "]"
$AdaptorInfo[0][7] = $objItem.DNSServerSearchOrder[$String1]
$String2 = "[" & UBound($objItem.DNSServerSearchOrder) -1 & "]"
$AdaptorInfo[0][8] = $objItem.DNSServerSearchOrder[$String2]
Endif
;....................................
Next
;....................................
Endif
;....................................
EndFunc

;------------------------------------------------------------------------------------------------------------

 

Share this post


Link to post
Share on other sites
10 hours ago, water said:

:)

Thanks "water" this only now works because of you.. So happy

I added the gateway now too. It seems if the NIC (network card gets no DHCP it has no gateway) SO this included a gateway now.

 

Just a FYI ;)

Oh de joy

 

;------------------------------------------------------------------------------------------------------------
; ------------------------
; TEST THE Primary and Secondry DNS SERVER
; ------------------------

Func _GETcurrentNICinfo2()  ; GET THE DNS SETTINGS
    $oErrors = ObjEvent("AutoIt.Error", "Error_Handle")
    Local $DnsSecondry
    Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" )
    Local $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
If @error Or NOT IsObj($query) Then  ; If Error or Nothing set
$AdaptorInfo[0][6] = ""
$AdaptorInfo[0][7] = ""
$AdaptorInfo[0][8] = ""
Endif
;....................................
If IsObj($query) Then
For $objItem In $query
;....................................
If UBound($objItem.DefaultIPGateway) = 1 then
$String0 = "[" & UBound($objItem.DefaultIPGateway) -1 & "]"
$AdaptorInfo[0][6] = $objItem.DefaultIPGateway[$String0]
Else
$AdaptorInfo[0][6] = ""
Endif
;....................................
If UBound($objItem.DNSServerSearchOrder) = 1 then                ; If only one DNS is set
$String1 = "[" & UBound($objItem.DNSServerSearchOrder) -1 & "]"
$AdaptorInfo[0][7] = $objItem.DNSServerSearchOrder[$String1]
Else
$AdaptorInfo[0][7] = ""
Endif
;....................................
If UBound($objItem.DNSServerSearchOrder) = 2 then                 ; If Two DNS is set
$String1 = "[" & UBound($objItem.DNSServerSearchOrder) -2 & "]"
$AdaptorInfo[0][7] = $objItem.DNSServerSearchOrder[$String1]
$String2 = "[" & UBound($objItem.DNSServerSearchOrder) -1 & "]"
$AdaptorInfo[0][8] = $objItem.DNSServerSearchOrder[$String2]
Endif
;....................................
Next
;....................................
Endif
;....................................
EndFunc

 

Share this post


Link to post
Share on other sites

and now with out the [] lol bugs.... this works

o:)

 

;------------------------------------------------------------------------------------------------------------
; ------------------------
; TEST THE Primary and Secondry DNS SERVER and GATEWAY Checker
; ------------------------

Func _GETcurrentNICinfo2()  ; GET THE DNS SETTINGS
    $oErrors = ObjEvent("AutoIt.Error", "Error_Handle")
    Local $DnsSecondry
    Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" )
    Local $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
If @error Or NOT IsObj($query) Then  ; If Error or Nothing set
$AdaptorInfo[0][6] = ""
$AdaptorInfo[0][7] = ""
$AdaptorInfo[0][8] = ""
Endif
;....................................
If IsObj($query) Then
For $objItem In $query
;....................................
If UBound($objItem.DefaultIPGateway) = 1 then                    ; Get the Default Gateway
$String0 = UBound($objItem.DefaultIPGateway) -1
$AdaptorInfo[0][6] = $objItem.DefaultIPGateway[$String0]
Else
$AdaptorInfo[0][6] = ""
Endif
;....................................
If UBound($objItem.DNSServerSearchOrder) = 1 then                ; If only one DNS is set
$String1 = UBound($objItem.DNSServerSearchOrder) -1
$AdaptorInfo[0][7] = $objItem.DNSServerSearchOrder[$String1]
$AdaptorInfo[0][8] = ""
Else
$AdaptorInfo[0][7] = ""
$AdaptorInfo[0][8] = ""
Endif
;....................................
If UBound($objItem.DNSServerSearchOrder) = 2 then                 ; If Two DNS is set
$String1 = UBound($objItem.DNSServerSearchOrder) -2
$AdaptorInfo[0][7] = $objItem.DNSServerSearchOrder[$String1]
$String2 = UBound($objItem.DNSServerSearchOrder) -1
$AdaptorInfo[0][8] = $objItem.DNSServerSearchOrder[$String2]
Endif
;....................................
Next
;....................................
Endif
;....................................
EndFunc

 

Edited by Dwalfware

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

  • Similar Content

    • By BetaLeaf
      What is Rollbar?
      Rollbar provides real-time error alerting & debugging tools for developers. Learn more about it at https://rollbar.com/product/
      Demo: https://rollbar.com/demo/demo/
      Screenshot:
      Instructions: (RollbarTest.au3)
      ; Include RollbarSDK #include "RollbarSDK.au3" ;Turns on ConsoleWrite debugging override. ;Global $Rollbar_Debug=False ; Initialize RollbarSDK with the project's API key. ; Parameters ....: $__Rollbar_sToken - [Required] Go to https://rollbar.com/<User>/<ProjectName>/settings/access_tokens/ for your project. Use the token for post_server_item. _Rollbar_Init("eaa8464a4082eeabd9454465b8f0c0af") ; Write code that causes an error you want to catch, then call ; _Rollbar_Send ; Parameters ....: $__Rollbar_sErrorLevel - [Required] Must be one of the following values: Debug, Info, Warning, Error, Critical. ; $__Rollbar_sMessage - [Required] The message to be sent. This should contain any useful debugging info that will help you debug. ; $__Rollbar_sMessageSummary - [Optional] A string that will be used as the title of the Item occurrences will be grouped into. Max length 255 characters. If omitted, Rollbar will determine this on the backend. _Rollbar_Send("Debug", "This is an debug message. If you received this, you were successful!", "Debug Message") _Rollbar_Send("Info", "This is a test message. If you received this, you were successful!", "Info Message") _Rollbar_Send("Warning", "This is an warning message. If you received this, you were successful!", "Warning Message") _Rollbar_Send("Error", "This is an error message. If you received this, you were successful!", "Error Message") _Rollbar_Send("Critical", "This is an critical message. If you received this, you were successful!", "Critical Message") _Rollbar_Send("Info", "This is a test message. If you received this, you were successful!") ;No Message ; Rollbar_Send's helper functions ; Parameters ....: $__Rollbar_sMessage - [Required] The message to be sent. This should contain any useful debugging info that will help you debug. ; $__Rollbar_sMessageSummary - [Optional] A string that will be used as the title of the Item occurrences will be grouped into. Max length 255 characters. If omitted, Rollbar will determine this on the backend. _Rollbar_SendDebug("This is an debug message. If you received this, you were successful!", "Debug Message") _Rollbar_SendInfo("This is a test message. If you received this, you were successful!", "Info Message") _Rollbar_SendWarning("This is an warning message. If you received this, you were successful!", "Warning Message") _Rollbar_SendError("This is an error message. If you received this, you were successful!", "Error Message") _Rollbar_SendCritical("This is an critical message. If you received this, you were successful!", "Critical Message") ; Usable Example Local $sImportantFile = "C:\NOTAREALFILE_1234554321.txt" Switch FileExists($sImportantFile) Case True MsgBox(0, "Example Script", "An important file was found. Continuing...") Case Else _Rollbar_SendCritical('An important file was missing. Halting... File: "' & $sImportantFile & '"', 'Important file "' & $sImportantFile & '" is missing.') EndSwitch Notes: Please comment your feedback, advice, & suggestions below. While this is only a proof of concept, I will expand its feature set for everyone to use. 
      Right now, it is fully functional but not tested in production.
       
       
      Changelog:
      RollbarSDK.au3
      RollbarTest.au3
      v0.2
       
      v0.1.1
       
    • By jresine
      Hello, is it possible to know via a script or command, to have the percentage of disk usage of a process? thank you in advance.
      ps: see image

    • By Dwalfware
      HI
      Another head scratched
      Anyone solve how to get the IPV6 from WMI?
      I followed the usual SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID='" & GUICtrlRead($cboAdapters) & "'
      get the index 
      port it to 
      Local $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
      All i can see is IPv4 addresses
      My question is is there another place the IPv6 information is hiding? I configured my LAN with IPV6 and my wireless with IPv4 so I can see what shows.
      Thanks in Advance.
      PS. I did try google
       
    • By j0kky
      Hi folks,
      Last morning I needed to know programmatically which were my DNS(s) for the current connection, and I searched for an API which fitted my needs... I just tried dnsqueryconfig, which shows the DNS(s) used, if you have manually selected them in the past. 
      The API is little tricky (or maybe I'm little rusty), so I decided to write a small UDF function to avoid a waste of time in the future... here you are.
      ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_DnsQueryConfig ; Description ...: Retrieves the currently used DNS servers, if they were selected by user ; Syntax.........: _WinAPI_DnsQueryConfig() ; Return values .: On success it returns an array with the list of currently used DNS servers ; ; On failure it returns 0 and sets @error to non zero (these values are useful only for debugging reasons): ; |1 - DllCall error ; |2 - Generic error, DNS could be generated automatically ; Author ........: j0kky ; Modified ......: 1.0.0 14/11/2018 ; Link ..........: https://docs.microsoft.com/en-us/windows/desktop/api/windns/nf-windns-dnsqueryconfig ; =============================================================================================================================== Func _WinAPI_DnsQueryConfig() Local Const $DnsConfigDnsServerList = 6 Local $aRet = DllCall("Dnsapi.dll", "LONG", "DnsQueryConfig", "int", $DnsConfigDnsServerList, "dword", 0, "ptr", Null, "ptr", 0, "ptr", Null, "dword*", 0) If @error Then Return SetError(1, 0, 0) if $aRet[6] <= 4 Then Return SetError(2, 0, 0) Local $tagBuffer = "" For $i = 1 To ($aRet[6] / 4) $tagBuffer &= "dword;" Next Local $tBuffer = DllStructCreate($tagBuffer) $aRet = DllCall("Dnsapi.dll", "LONG", "DnsQueryConfig", "int", $DnsConfigDnsServerList, "dword", 0, "ptr", Null, "ptr", 0, "ptr", DllStructGetPtr($tBuffer), "dword*", $aRet[6]) Local $aDNS[($aRet[6] / 4) - 1] For $i = 2 to (UBound($aDNS) + 1) $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "dword", DllStructGetData($tBuffer, $i)) if @error Then Return SetError(1, 0, 0) $aDNS[$i - 2] = $aRet[0] Next Return SetError(0, 0, $aDNS) EndFunc
    • By vati1011
      Hey, guys.
      Currently, I'm using Opencv UDF and i'm  encountering issues with DLLOPEN(). it is failing and returning -1
      But, I cannot figure out why it is failing.
      I need help from people.
      I'm running windows 10 32bit.
      This's my code.
      #RequireAdmin $fileopen = DllOpen("opencv_core2413.dll") MsgBox(0,"Result",$fileopen) Example.au3opencv_core2413.dll
×
×
  • Create New...