Jump to content

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

 

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

Link to post
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

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

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

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

 

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

 

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
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By hek
      Hey everyone,
      Was wondering how I would be able to implement this on a local computer instead of using connectserver? 
      Any suggestions or help would be appreciated. Thanks. 
    • By DirtyJohny
      Hi everyone.Need rewrite this function how in еxample.
      Original:
      #RequireAdmin #NoTrayIcon Opt("MustDeclareVars",1) Func _a() Local $sls=ObjGet("winmgmts:{impersonationLevel=impersonate," _ &"authenticationLevel=Pkt}!\\"& _ @ComputerName&'\root\wmi'),$lss=$sls.ExecQuery _ ('SELECT * FROM WmiMonitorID'), _ $lll,$sll,$sss="",$lsl,$lls,$i,$z For $z In $lss $lsl=$z.UserFriendlyName For $i=0 To Ubound($lsl)-1 if ($lsl[$i]) Then $lll&=Chr($lsl[$i]) Next $lls=$z.SerialNumberID For $i=0 To Ubound($lls)-1 if ($lls[$i]) Then $sll&=Chr($lls[$i]) Next $sss&="Model"&@TAB&@TAB&": "&$lll&@CR&"Serial Number"&@TAB&": "&$sll&@CR&@CR $lll="" $sll="" Next MsgBox(262144,'Monitors '&$lss.Count,$sss&" "&@CR) $lss=Null $sls=Null EndFunc _a() Example:
      Func _InfoPC() Local $ObjService = ObjGet('winmgmts:{impersonationLevel = impersonate}!\\' & @ComputerName & '\root\cimv2') Local $ObjMB = $ObjService.ExecQuery('SELECT * FROM Win32_BaseBoard', 'WQL', 0x30) If IsObj($ObjService) Then For $objItemMB In $ObjMB $sInfo &= @TAB & 'Motherboard: ...... ' & $objItemMB.Product & @CRLF I'm beginner in this sphere and need  you all speak easy and simply because i'm Russian.Thanks)
    • By jguinch
      Hello.
      I did create these few functions several months ago. I post here, if it can interest someone.
      These functions based on WMI queries allow you to manage printers : add / delete printer, driver, port, or obtain configuration, set default printer ... I let you discover it with the code.

       
      Here is the list of the available functions :
      _Printmgr_AddLocalPort
      _Printmgr_AddLPRPort
      _PrintMgr_AddPrinter
      _PrintMgr_AddPrinterDriver
      _PrintMgr_AddTCPIPPrinterPort
      _PrintMgr_AddWindowsPrinterConnection
      _PrintMgr_CancelAllJobs
      _Printmgr_EnumPorts
      _PrintMgr_EnumPrinter
      _PrintMgr_EnumPrinterConfiguration
      _PrintMgr_EnumPrinterDriver
      _PrintMgr_EnumPrinterProperties
      _PrintMgr_EnumTCPIPPrinterPort
      _Printmgr_Pause
      _Printmgr_PortExists
      _Printmgr_PrinterExists
      _Printmgr_PrinterSetComment
      _Printmgr_PrinterSetDriver
      _Printmgr_PrinterSetPort
      _Printmgr_PrinterShare
      _Printmgr_PrintTestPage
      _PrintMgr_RemoveLocalPort
      _PrintMgr_RemoveLPRPort
      _PrintMgr_RemovePrinter
      _PrintMgr_RemovePrinterDriver
      _PrintMgr_RemoveTCPIPPrinterPort
      _PrintMgr_RenamePrinter
      _Printmgr_Resume
      _PrintMgr_SetDefaultPrinter
       
      And some examples :
      #include <Array.au3> #include "PrintMgr.au3" _Example() Func _Example() ; Remove a printer called "My old Lexmark printer" : _PrintMgr_RemovePrinter("My old Lexmark printer") ; Remove the driver called "Lexmark T640" : _PrintMgr_RemovePrinterDriver("Lexmark T640") ; Remove the TCP/IP printer port called "TCP/IP" _PrintMgr_RemoveTCPIPPrinterPort("MyOLDPrinterPort") ; Add a driver, called "Samsung ML-451x 501x Series", and driver inf file is ".\Samsung5010\sse2m.inf" _PrintMgr_AddPrinterDriver("Samsung ML-451x 501x Series", "Windows NT x86", @ScriptDir & "\Samsung5010", @ScriptDir & "\Samsung5010\sse2m.inf") ; Add a TCP/IP printer port, called "MyTCPIPPrinterPort", with IPAddress = 192.168.1.10 and Port = 9100 _PrintMgr_AddTCPIPPrinterPort("MyTCPIPPrinterPort", "192.168.1.10", 9100) ; Add a printer, give it the name "My Printer", use the driver called "Samsung ML-451x 501x Series" and the port called "MyTCPIPPrinterPort" _PrintMgr_AddPrinter("My Printer", "Samsung ML-451x 501x Series", "MyTCPIPPrinterPort") ; Set the printer called "My Printer" as default printer _PrintMgr_SetDefaultPrinter("My Printer") ; Connect to the shared printer "\\192.168.1.1\HPDeskjetColor") _PrintMgr_AddWindowsPrinterConnection("\\192.168.1.1\HPDeskjetColor") ; List all installed printers Local $aPrinterList = _PrintMgr_EnumPrinter() _ArrayDisplay($aPrinterList) ; List all printers configuration Local $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration() _ArrayDisplay($aPrinterConfig) ; List all installed printer drivers Local $aDriverList = _PrintMgr_EnumPrinterDriver() _ArrayDisplay($aDriverList) ; Retrieve the printer configuration for the printer called "Lexmark T640" $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration("Lexmark T640") _ArrayDisplay($aPrinterConfig) ; Add a local printer port (for a file output) _PrintMgr_AddLocalPort("c:\temp\output.pcl") ; Remove the local port _PrintMgr_RemoveLocalPort("c:\temp\output.pcl") EndFunc ;==>_Example Download link :
      PrintMgr.au3
      PrintMgr_Example.au3
    • By shital
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** RunAs(test", @ComputerName, "testinng",2,"wmic product where ""name= '%notepadexamples%'"" call uninstall", @SystemDir & "\wbem", @SW_MAXIMIZE)  it not working 
       
    • By Colduction
      Hi AutoIt Programmers!

      I've seen a code that gives Name of Group by writing it's group's SID, but this one response very slower than Windows Command-Line WMIC
       
      Func _GetLocalGroupName($sSID = 'S-1-5-18') $objWMIService = ObjGet ("winmgmts:\\" & @ComputerName & "\root\cimv2") $colItems = $objWMIService.ExecQuery('SELECT Name FROM Win32_Group where SID="' & $sSID & '"') For $GroupNames in $colItems MsgBox (0,"",$GroupNames.Name) ExitLoop Next EndFunc I don't want call and use any third-party programs even CMD, i just want use from Windows API, netapi32.dll or AutoIt Functions (Standalone).
      Do you have any idea to improve speed/performance of this code? I'll happy of your comments
       
       
       
      ====================== SOLOUTION by @Subz ======================
       
×
×
  • Create New...