Sign in to follow this  
Followers 0
deadserious

Function in Loop is it possible?

5 posts in this topic

#1 ·  Posted (edited)

Hello I'm stumped and I'm not sure if this is possible. I'm trying to pull Systems from a text file and run a number of queries and pipe the info back to an excel spreadsheet, however I'm not sure if you can add a function to a loop as listed below.

 

Does anyone have any experience with this that they can point me in the right direction?

I would really appreciate it since I'm just learning this autoit... Nice stuff but can be complicated at times. Thx... :-)

 

 
#include <Excel.au3>
Local $UserName = "Administrator"
$Password = "Password"
Local $csv = FileOpen(@ScriptDir & "\SystemInfo.csv", 1)
Local $strName = FileOpen(@ScriptDir & "\Computers.txt",0)
 
While 1
   Local $strComputerName = FileReadLine($strName)
   If @error = -1 Then ExitLoop
   MsgBox(0,"Computer Name", "This is the computer name: " & $strComputerName)
   DriveMapAdd("Z:", "\\" & $strComputerName & "\C$", 8, $strComputerName & "\" & $UserName, $Password)
  
Func _Get_Vergence_Ver()
   $Vergence = FileGetVersion("Z:\Program Files\Sentillion\Vergence Authenticator\Authenticator.exe")
   Return $Vergence
EndFunc
Func _Get_Name()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT Name FROM Win32_ComputerSystem", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
   Local $s_Text = $ObjItem.name
  Next
       
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc
 
Func _Get_SystemVendor()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
   Local $s_Text = $ObjItem.Vendor
  Next
       
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc
 
 
Func _Get_SystemVersion()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
   Local $s_Text = $ObjItem.Name
  Next
       
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc
 
Func _Get_SystemVModel()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
   Local $s_Text = $ObjItem.Version
  Next
       
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc
 
 Func _Get_OS_Name()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
   Local $s_Text = $ObjItem.Caption
  Next
       
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc
; Writes information to the Excel Spreadsheet in Script Director.
FileWrite($csv, "ComputerName" & "," & "Vendor" & "," & "Model" & "," & "OS" & "," & "Vergence Version" & "," & @CRLF & _Get_Name() & "," & _Get_SystemVendor() & "," & _Get_SystemVersion() & "," & _Get_OS_Name() & "," & _Get_Vergence_Ver() & @CRLF)
FileClose($csv)
;MsgBox(0, "Script finished", "The script named: " & @ScriptName & " finished!", 10)
; Opens file once the script has finished running.
$oExcelExist = FileExists(@ScriptDir & "\SystemInfo.csv")
If $oExcelExist = 1 Then
   _ExcelBookOpen(@ScriptDir & "\SystemInfo.csv")
   EndIf
   If @error = 1 Then
   MsgBox(0, "Error!", "Unable to Open the Excel Object")
   Exit
   ElseIf @error = 2 Then
   MsgBox(0, "Error!", "Cannot Open " $oExcelExist)
   Exit
   EndIf
WEnd
FileClose($csv)
; Opens file once the script has finished running.
$oExcelExist = FileExists(@ScriptDir & "\SystemInfo.csv")
If $oExcelExist = 1 Then
   _ExcelBookOpen(@ScriptDir & "\SystemInfo.csv")
   EndIf
   If @error = 1 Then
   MsgBox(0, "Error!", "Unable to Open the Excel Object")
   Exit
   ElseIf @error = 2 Then
   MsgBox(0, "Error!", "Cannot Open " $oExcelExist)
   Exit
   EndIf
DriveMapDel("Z:")
FileClose($strName)
Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites



Functions should reside outside of loops. I re-arranged your code below. AutoIt will take each line of code in sequential order, so I arranged it to:

1. open the files

2. run the loop. inside the loop, it will call those 6 functions, one after another, on the line that writes to the csv file.

3. it closes the files

4. it opens the file it wrote to

I assume that your functions work correctly. I have not tested this code. I hope it helps point you in the right direction.

#include <Excel.au3>
Local $UserName = "Administrator"
$Password = "Password"

Local $csv = FileOpen(@ScriptDir & "\SystemInfo.csv", 1)
Local $strName = FileOpen(@ScriptDir & "\Computers.txt", 0)
DriveMapAdd("Z:", "\\" & $strComputerName & "\C$", 8, $strComputerName & "\" & $UserName, $Password) ; pulled this outside the loop because you only need to map it once

While 1
    Local $strComputerName = FileReadLine($strName)
    If @error = -1 Then ExitLoop
;~     MsgBox(0, "Computer Name", "This is the computer name: " & $strComputerName)
    
    ; Writes information to the Excel Spreadsheet in Script Director.
    FileWrite($csv, "ComputerName" & "," & "Vendor" & "," & "Model" & "," & "OS" & "," & "Vergence Version" & "," & @CRLF & _Get_Name() & "," & _Get_SystemVendor() & "," & _Get_SystemVersion() & "," & _Get_OS_Name() & "," & _Get_Vergence_Ver() & @CRLF)
WEnd

 FileClose($csv) ; pulled this outside the loop because you only need to close once the loop is finished
 FileClose($strName) ; same for this one
 
; Opens file once the script has finished running.
$oExcelExist = FileExists($csv)
If $oExcelExist = 1 Then
    _ExcelBookOpen($csv)
EndIf
If @error = 1 Then
    MsgBox(0, "Error!", "Unable to Open the Excel Object")
    Exit
ElseIf @error = 2 Then
    MsgBox(0, "Error!", "Cannot Open " $oExcelExist)
    Exit
EndIf

DriveMapDel("Z:")



;;;;;;;;;;;;;;;  Begin Functions  ;;;;;;;;;;;;;;;;

Func _Get_Vergence_Ver()
    $Vergence = FileGetVersion("Z:\Program Files\Sentillion\Vergence Authenticator\Authenticator.exe")
    Return $Vergence
EndFunc   ;==>_Get_Vergence_Ver

Func _Get_Name()
    Local $s_Text = ''
    $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
    $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
    $colItems = $objWMIService.ExecQuery("SELECT Name FROM Win32_ComputerSystem", "WQL", 0x30)
    If (IsObj($objWMIService)) Then
        For $objItem In $colItems
            Local $s_Text = $objItem.name
        Next

        Return String($s_Text)
    Else
        Return 0
    EndIf
EndFunc   ;==>_Get_Name

Func _Get_SystemVendor()
    Local $s_Text = ''
    $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
    $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
    If (IsObj($objWMIService)) Then
        For $objItem In $colItems
            Local $s_Text = $objItem.Vendor
        Next

        Return String($s_Text)
    Else
        Return 0
    EndIf
EndFunc   ;==>_Get_SystemVendor

Func _Get_SystemVersion()
    Local $s_Text = ''
    $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
    $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
    If (IsObj($objWMIService)) Then
        For $objItem In $colItems
            Local $s_Text = $objItem.Name
        Next

        Return String($s_Text)
    Else
        Return 0
    EndIf
EndFunc   ;==>_Get_SystemVersion

Func _Get_SystemVModel()
    Local $s_Text = ''
    $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
    $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
    If (IsObj($objWMIService)) Then
        For $objItem In $colItems
            Local $s_Text = $objItem.Version
        Next

        Return String($s_Text)
    Else
        Return 0
    EndIf
EndFunc   ;==>_Get_SystemVModel

Func _Get_OS_Name()
    Local $s_Text = ''
    $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
    $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", 0x30)
    If (IsObj($objWMIService)) Then
        For $objItem In $colItems
            Local $s_Text = $objItem.Caption
        Next

        Return String($s_Text)
    Else
        Return 0
    EndIf
EndFunc   ;==>_Get_OS_Name

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

deadserious,

When you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

abberration Thanks for the help with this, I'm still trying to learn Autoit.
The direction that you gave me helped me greatly. I appreciate your wisdom here.
 
I do have another issue if you don't mind me asking?
When I run the code it works on the list of computers that I'm running it against, however when it finds a computer in the list that has a diffent password it errors out.
 
Is there a way to state IF At Error continue script?
Your input is greatly appreciated.
 
 I attached the error just under the script, thanks again...
 


#include <Excel.au3>
Local $UserName = "Administrator"
Local $Password = InputBox("Enter Password", "Please enter a Password for the remote PC.")

Local $csv = FileOpen(@ScriptDir & "\Get-System-Info-ICA-FromList.csv", 1)
Local $strName = FileOpen(@ScriptDir & "\Computers.txt",0)
If $csv = -1 Then
   MsgBox(16, "Error", "Unable to open Get-System-Info-ICA-FromList.csv file to write report." & @CRLF & "Please check to see if it is already open.")
   Exit
EndIf
If $strName = -1 Then
   MsgBox(48, "Error", "Error Reading Computers.txt for input of computer list." & @CRLF & @CRLF & "You must have a Computers.txt File with a list of" & _ 
   " Computers to read from in the directory where this script resides.")
   Exit
EndIf
FileWrite($csv, "Computer Name" & "," & "Vergence Version" & "," & "Citrix Version" & "," & "Manufacturer" & "," & "Model" & "," & "Operating System" & @CRLF)
While 1
   Local $strComputerName = FileReadLine($strName)
   If @error = -1 Then ExitLoop
      FileWrite($csv, _Get_Name() & "," & _Get_Vergence_Ver() & ", " & _Get_ICA_Ver() & "," & _Get_SystemVendor() & "," & _Get_SystemVersion() & "," & _Get_OS_Name() & @CRLF)    
      DriveMapAdd("Z:", "\\" & $strComputerName & "\C$", 8, $strComputerName & "\" & $UserName, $Password)
      WEnd
      
      
; ========= Function Start ===========>
Func _Get_Vergence_Ver()
   $Vergence = FileGetVersion("Z:\Program Files\Sentillion\Vergence Authenticator\Authenticator.exe")
   Return $Vergence
EndFunc

Func _Get_ICA_Ver()
   $ICA = FileGetVersion("Z:\Program Files\Citrix\ICA Client\wfica32.exe")
   Return $ICA
EndFunc


Func _Get_Name()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT Name FROM Win32_ComputerSystem", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
      Local $s_Text = $ObjItem.name
     Next
        
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc 
 
 
 
 Func _Get_SystemVendor()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
      Local $s_Text = $ObjItem.Vendor
     Next
        
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc 
 

 
Func _Get_SystemVersion()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
      Local $s_Text = $ObjItem.Name
     Next
        
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc 


Func _Get_System_Model()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
      Local $s_Text = $ObjItem.Version
     Next
        
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc 

 
 Func _Get_OS_Name()
Local $s_Text = ''
$objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", 0x30)
If (IsObj($objWMIService)) Then
   For $objItem In $colItems
      Local $s_Text = $ObjItem.Caption
     Next
        
        Return String($s_Text)
    Else
        Return 0
    EndIf
 EndFunc 
 
 DriveMapDel("Z:")
 
 
;=========== Function End ===========>

FileClose($csv)

; Opens file once the script has finished running.
$oExcelExist = FileExists(@ScriptDir & "\Get-System-Info-ICA-FromList.csv")
If $oExcelExist = 1 Then
   _ExcelBookOpen(@ScriptDir & "\Get-System-Info-ICA-FromList.csv")
   EndIf
   If @error = 1 Then
      MsgBox(0, "Error!", "Unable to Open the Excel Get-System-Info-ICA-FromList.csv file at the root of the script directory.")
      Exit
   ElseIf @error = 2 Then
      MsgBox(0, "Error!", "Cannot Open " $oExcelExist)
      Exit
   EndIf


FileClose($strName)

 Here is the error I get:

H:ScriptingAutoItGetSystemInfoICAGet-System-Info-ICA-FromList.au3 (51) : ==> The requested action with this object has failed.:
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "rootcimv2", $strComputerName & "" & $UserName, $Password, "", "", "&H80")
$objWMIService = $objWMILocator.ConnectServer($strComputerName, "rootcimv2", $strComputerName & "" & $UserName, $Password, "", "", "&H80")^ ERROR
>Exit code: 1    Time: 125.520

 
 

Share this post


Link to post
Share on other sites

deadserious,

When you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

Melba23, thanks for letting me know how this is done, I've only posted a couple of times. Your direction has made the posting better to view.

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  
Followers 0