Jump to content

Variable must be of type "Object" inside loop


Recommended Posts

Hi

I need help, I found script to check computer model. I want use it in loop to chceck multiple pc`s (targets.txt with computer names).

But when i try use inside loop i get error: Variable must be of type "Object" and I dont know how to fix this :(

Plz help

 

Local $targets = @ScriptDir& "\targets.txt"
Local $log_name = "\scan.log"

$msgBox = MsgBox(4, "Scan", "need tergets.txt")

Example()

Func Example()

   If $msgBox = 7 Then
      exit
   EndIf

   If Not FileExists($targets) Then
      MsgBox($MB_SYSTEMMODAL, "", "File: targets.txt - no exist !")
      Exit
   EndIf

   FileOpen($targets, 0)
   Global $arr[1000]
   ReDim $arr[_FileCountLines($targets)+1]

   For $i = 1 to _FileCountLines($targets)
       $line = FileReadLine($targets, $i)
       $arr[$i] = $line

      ; chceck if pc is online
      Local $iPing = Ping($arr[$i], 250)
      If $iPing Then
         ; ONLINE
         Local $strComputer = $arr[$i]

         $colItems = ""

         $Output=""
         $Output = $Output & "Computer: " & $strComputer  & @CRLF
         $Output = $Output & "==========================================" & @CRLF
         $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
         $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
                                                 "0x10" + "0x20")

         If IsObj($colItems) then
            For $objItem In $colItems
               $Output = $Output & "Vendor: " & $objItem.Vendor & @CRLF
               $Output = $Output & "SN: " & $objItem.IdentifyingNumber & @CRLF
               $Output = $Output & "Name: " & $objItem.Name & @CRLF
               $Output = $Output & "UUID: " & $objItem.UUID & @CRLF
             if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
             $Output=""
            Next
         Else
            Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_ComputerSystemProduct" )
         Endif

      Else
         ;OFFLINE
        _FileWriteLog(@ScriptDir & $log_name, $arr[$i]&" OFFLINE")
      EndIf
   Next


EndFunc

 

 

Link to comment
Share on other sites

Can you please post the error message from the SciTE output pane?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

  • Moderators

@harvester2001 the error is telling you that one of the objects does not exist. You have IsObj statement for your query, but not for the connection to WMI. Try something like this to catch the error (you'll have to massage it a little for your needs):

#include <File.au3>
#include <MsgBoxConstants.au3>

Local $oWMI, $oItems
Local $aTargets = FileReadToArray(@DesktopDir & "\PCs.txt")
    If IsArray($aTargets) Then
        For $sPC In $aTargets
            If Ping($sPC, 250) Then
                $oWMI = ObjGet("winmgmts:\\" & $sPC & "\root\CIMV2")
                    If IsObj($oWMI) Then
                        $oItems = $oWMI.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", "0x10" + "0x20")
                            If IsObj($oItems) Then
                                For $sOS In $oItems
                                    ConsoleWrite("Vendor: " & $sOS.Vendor & @CRLF & _
                                                 "SN: " & $sOS.IdentifyingNumber & @CRLF & _
                                                 "Name: " & $sOS.Name & @CRLF & _
                                                 "UUID: " & $sOS.UUID & @CRLF)
                                Next
                            Else
                                Msgbox($MB_OK,"WMI Output","No WMI Objects Found for class: " & "Win32_ComputerSystemProduct" )
                            Endif
                    Else
                        MsgBox($MB_OK, "WMI Output", "Could not connect to WMI on " & $sPC)
                    EndIf
            Else
                ;OFFLINE
                _FileWriteLog(@ScriptDir & "\Mylog.log", $sPC & " OFFLINE")
            EndIf
        Next
    EndIf

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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...