Jump to content

WMI Error on second object (DNS)


 Share

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

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

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

 

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

 

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

×
×
  • Create New...