Jump to content
Sign in to follow this  
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 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - 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 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - 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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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 ======================
       
    • By EmilyLove
      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
       
×
×
  • Create New...