Jump to content
Sign in to follow this  
Bradman

Loop Nesting question

Recommended Posts

Bradman

I have a script that looks up Registry Keys on remote systems to see if software is installed. The script logs if the software is installed or not and if the system is on-line or not.

The problem I have is trying to nest the registry search function within the read file function (system names from text file).

Here is a sample of the code I use when manually entering the system name and software key. This one works fine.

$g_szVersion = "My Script 103.9.002"

If WinExists($g_szVersion) Then Exit

AutoItWinSetTitle($g_szVersion)

#include <file.au3>

#include <Date.au3>

Global $i,$Found,$software,$system

$software = InputBox("ENTER SEARCH INFO", "YOU MUST ENTER THE EXACT REGISTRY KEY NAME FOR THE SOFTWARE YOU ARE LOOKING FOR!", "", "")

Sleep(1000)

$system = InputBox("ENTER SEARCH INFO", "ENTER SYSTEM NAME!", "", "")

While 1

$i=$i+1

$var = RegEnumKey("\\"& $system & "\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", $i)

If @error <> 0 then

MsgBox(4096,@UserName, $software & " IS NOT INSTALLED OR INCORRECT NAME!")

$file3 = FileOpen(@ScriptDir & "\NO_ACCESS_LOG.txt", 9)

_FileWriteToLine (@ScriptDir & "\NO_ACCESS_LOG.txt", 1, "USER:" & "," & "SYSTEM NAME:" & "," & "DATE:" & "," & "TIME:" & "," & "ACTION:", 1)

FileWrite($file3,@UserName & "," & $system & "," & (_NowDate()) & "," &(_NowtIME(5))&"," & "INCORRECT SOFTWARE NAME OR SYSTEM NOT ON LINE" & @CRLF)

FileClose($file3)

ExitLoop

Endif

If $var=$software Then

$Found=1

If $Found=1 Then

MsgBox(4096, "SubKey #" & $i,$VAR & " IS INSTALLED")

$file2 = FileOpen(@ScriptDir & "\INSTALL_LOG.txt", 9)

_FileWriteToLine (@ScriptDir & "\INSTALL_LOG.txt", 1, "USER:" & "," & "SYSTEM NAME:" & "," & "DATE:" & "," & "TIME:" & "," & "ACTION:", 1)

FileWrite($file2,@UserName & "," & $system & "," & (_NowDate()) & "," &(_NowtIME(5))&"," & $var & " IS INSTALLED" & @CRLF)

FileClose($file2)

ExitLoop

EndIf

EndIf

WEnd

Here is the sample I am trying to use to automate the system name function. It gives me the MsgBox(0, 'WARNING', "SYSTEM OFFLINE " & $line,2) message.

I am not familiar with nesting as you can see. I need help with the code.

$g_szVersion = "My Script 1.09.03.056"

If WinExists($g_szVersion) Then Exit

AutoItWinSetTitle($g_szVersion)

#include <file.au3>

#include <Date.au3>

Global $i,$Found,$software,$system

$software = InputBox("ENTER SEARCH INFO", "YOU MUST ENTER THE EXACT REGISTRY KEY NAME!", "", "")

Sleep(250)

$file = FileOpen(@ScriptDir & "\SYS_NAMES.txt", 0)

If $file =1 Then

While 1

$line = FileReadLine($file)

If @error = -1 Then

MsgBox(0, 'WARNING', 'UNABLE TO READ FILE OR END OF FILE!', 5)

ExitLoop

EndIf

Sleep(250)

MsgBox(0, 'WARNING', "ACCESSING SYSTEM " & $line,2)

If FileExists("\\"& $line & "\C:42507")Then

$file1 = FileOpen(@ScriptDir & "\ACCESS_LOG.txt", 9)

_FileWriteToLine (@ScriptDir & "\ACCESS_LOG.txt", 1, "USER:" & "," & "SYSTEM NAME:" & "," & "DATE:" & "," & "TIME:" & "," & "ACTION:", 1)

FileWrite($file1,@UserName & "," & $line & "," & (_NowDate()) & "," &(_NowtIME(5))&"," & "SYSTEM ON LINE" & @CRLF)

FileClose($file1)

While 1

$i=$i+1

$var = RegEnumKey("\\"& $line & "\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", $i)

If @error <> 0 then

MsgBox(4096,@UserName, $software & " IS NOT INSTALLED OR INCORRECT NAME!")

$file2 = FileOpen(@ScriptDir & "\NO_ACCESS_LOG.txt", 9)

_FileWriteToLine (@ScriptDir & "\NO_ACCESS_LOG.txt", 1, "USER:" & "," & "SYSTEM NAME:" & "," & "DATE:" & "," & "TIME:" & "," & "ACTION:", 1)

FileWrite($file2,@UserName & "," & $system & "," & (_NowDate()) & "," &(_NowtIME(5))&"," & "INCORRECT SOFTWARE NAME" & @CRLF)

FileClose($file2)

ExitLoop

Endif

If $var=$software Then

$Found=1

If $Found=1 Then

MsgBox(4096, "SubKey #" & $i,$VAR & " IS INSTALLED")

$file3 = FileOpen(@ScriptDir & "\INSTALL_LOG.txt", 9)

_FileWriteToLine (@ScriptDir & "\INSTALL_LOG.txt", 1, "USER:" & "," & "SYSTEM NAME:" & "," & "DATE:" & "," & "TIME:" & "," & "ACTION:", 1)

FileWrite($file3,@UserName & "," & $system & "," & (_NowDate()) & "," &(_NowtIME(5))&"," & $var & " IS INSTALLED" & @CRLF)

FileClose($file3)

ExitLoop

EndIf

EndIf

WEnd

EndIf

If Not FileExists("\\"& $line & "\C:42507")Then

MsgBox(0, 'WARNING', "SYSTEM OFFLINE " & $line,2)

$file2 = FileOpen(@ScriptDir & "\NO_ACCESS_LOG.txt", 9)

_FileWriteToLine (@ScriptDir & "\NO_ACCESS_LOG.txt", 1, "USER:" & "," & "SYSTEM NAME:" & "," & "DATE:" & "," & "TIME:" & "," & "ACTION:", 1)

FileWrite($file2,@UserName & "," & $line & "," & (_NowDate()) & "," &(_NowtIME(5))&"," & "SYSTEM OFFLINE" & @CRLF)

FileClose($file2)

ContinueLoop

EndIf

WEnd

Else

Exit

EndIf

FileClose($file)

Any help would be nice.

Thanks Bradman

Share this post


Link to post
Share on other sites
Valuater

Maybe...

per help...

****************

FileOpen ( "filename", mode )

Return Value

Success: Returns a file "handle" for use with subsequent file functions. Failure: Returns -1 if error occurs.

*****************

you have ...

$file = FileOpen(@ScriptDir & "\SYS_NAMES.txt", 0)

If $file = 1 Then

If the file was opened correctly, then there would not be a "1"

file open ... per help

[/b]$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open  file.")
    Exit
EndIf

FileClose($file)

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
PsaltyDS

It's not so bad. You used a redundant If/Then, If/Then and I replaced it with one If/Else. I also tweaked the logging to take advantage of AutoIt's _FileWriteLog(), which does the open/write/close for you and adds a time tag automatically to boot. The biggest thing is to simply do this in SciTE and run Tidy on it!

#include <file.au3>
#include <Date.au3>

$g_szVersion = "My Script 1.09.03.056"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)

Global $i, $Found, $software, $system

; Initialize logs
Global $AccessLog = @ScriptDir & "\ACCESS_LOG.txt"
_FileWriteLog($AccessLog, "USER:,SYSTEM NAME:,ACTION:")
_FileWriteLog($AccessLog, @UserName & "," & @ComputerName & "," & "Initialized log file")
Global $NoAccessLog = @ScriptDir & "\NO_ACCESS_LOG.txt"
_FileWriteLog($NoAccessLog, "USER:,SYSTEM NAME:,ACTION:")
_FileWriteLog($NoAccessLog, @UserName & "," & @ComputerName & "," & "Initialized log file")
Global $InstallLog = @ScriptDir & "\INSTALL_LOG.txt"
_FileWriteLog($InstallLog, "USER:,SYSTEM NAME:,ACTION:")
_FileWriteLog($InstallLog, @UserName & "," & @ComputerName & "," & "Initialized log file")

$software = InputBox("ENTER SEARCH INFO", "YOU MUST ENTER THE EXACT REGISTRY KEY NAME!", "", "")
Sleep(250)

$file = FileOpen(@ScriptDir & "\SYS_NAMES.txt", 0)

If $file <> -1 Then
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then
            MsgBox(0, 'WARNING', 'UNABLE TO READ FILE OR END OF FILE!', 5)
            FileClose($file)
            ExitLoop
        EndIf
        Sleep(250)
        MsgBox(0, 'WARNING', "ACCESSING SYSTEM " & $line, 2)
        If FileExists("\\" & $line & "\C:42507") Then
            _FileWriteLog($AccessLog, @UserName & "," & $line & "," & "SYSTEM ON LINE")
            While 1
                $i = $i + 1
                $var = RegEnumKey("\\" & $line & "\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", $i)
                If @error <> 0 Then
                    MsgBox(4096, @UserName, $software & " IS NOT INSTALLED OR INCORRECT NAME!")
                    _FileWriteLog($NoAccessLog, @UserName & "," & $line & "," & "INCORRECT SOFTWARE NAME")
                    ExitLoop
                EndIf
                If $var = $software Then
                    $Found = 1
                    If $Found = 1 Then
                        MsgBox(4096, "SubKey #" & $i, $var & " IS INSTALLED")
                        _FileWriteLog($InstallLog, @UserName & "," & $system & "," & $var & " IS INSTALLED")
                        ExitLoop
                    EndIf
                EndIf
            WEnd
        Else
            MsgBox(0, 'WARNING', "SYSTEM OFFLINE " & $line, 2)
            _FileWriteLog($NoAccessLog, @UserName & "," & $line & "," & "SYSTEM OFFLINE")
            ContinueLoop
        EndIf
    WEnd
Else
    Exit
EndIf
FileClose($file)

So the question is... does it work?

:)

Edit: Tweaked FileOpen and added a FileClose.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Bradman

It's not so bad. You used a redundant If/Then, If/Then and I replaced it with one If/Else. I also tweaked the logging to take advantage of AutoIt's _FileWriteLog(), which does the open/write/close for you and adds a time tag automatically to boot. The biggest thing is to simply do this in SciTE and run Tidy on it!

#include <file.au3>
#include <Date.au3>

$g_szVersion = "My Script 1.09.03.056"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)

Global $i, $Found, $software, $system

; Initialize logs
Global $AccessLog = @ScriptDir & "\ACCESS_LOG.txt"
_FileWriteLog($AccessLog, "USER:,SYSTEM NAME:,ACTION:")
_FileWriteLog($AccessLog, @UserName & "," & @ComputerName & "," & "Initialized log file")
Global $NoAccessLog = @ScriptDir & "\NO_ACCESS_LOG.txt"
_FileWriteLog($NoAccessLog, "USER:,SYSTEM NAME:,ACTION:")
_FileWriteLog($NoAccessLog, @UserName & "," & @ComputerName & "," & "Initialized log file")
Global $InstallLog = @ScriptDir & "\INSTALL_LOG.txt"
_FileWriteLog($InstallLog, "USER:,SYSTEM NAME:,ACTION:")
_FileWriteLog($InstallLog, @UserName & "," & @ComputerName & "," & "Initialized log file")

$software = InputBox("ENTER SEARCH INFO", "YOU MUST ENTER THE EXACT REGISTRY KEY NAME!", "", "")
Sleep(250)

$file = FileOpen(@ScriptDir & "\SYS_NAMES.txt", 0)

If $file <> -1 Then
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then
            MsgBox(0, 'WARNING', 'UNABLE TO READ FILE OR END OF FILE!', 5)
            FileClose($file)
            ExitLoop
        EndIf
        Sleep(250)
        MsgBox(0, 'WARNING', "ACCESSING SYSTEM " & $line, 2)
        If FileExists("\\" & $line & "\C:42507") Then
            _FileWriteLog($AccessLog, @UserName & "," & $line & "," & "SYSTEM ON LINE")
            While 1
                $i = $i + 1
                $var = RegEnumKey("\\" & $line & "\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", $i)
                If @error <> 0 Then
                    MsgBox(4096, @UserName, $software & " IS NOT INSTALLED OR INCORRECT NAME!")
                    _FileWriteLog($NoAccessLog, @UserName & "," & $line & "," & "INCORRECT SOFTWARE NAME")
                    ExitLoop
                EndIf
                If $var = $software Then
                    $Found = 1
                    If $Found = 1 Then
                        MsgBox(4096, "SubKey #" & $i, $var & " IS INSTALLED")
                        _FileWriteLog($InstallLog, @UserName & "," & $system & "," & $var & " IS INSTALLED")
                        ExitLoop
                    EndIf
                EndIf
            WEnd
        Else
            MsgBox(0, 'WARNING', "SYSTEM OFFLINE " & $line, 2)
            _FileWriteLog($NoAccessLog, @UserName & "," & $line & "," & "SYSTEM OFFLINE")
            ContinueLoop
        EndIf
    WEnd
Else
    Exit
EndIf
FileClose($file)

So the question is... does it work?

:)

Edit: Tweaked FileOpen and added a FileClose.

Nice to have the script looked at, I completely forgot about the log file function. The only thing is I need comma delimited so I can pull the log file into a database. I think this will work.

Thanks for the help.

Bradman

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  

×