Jump to content

Check on called program


Recommended Posts

I am writing a script for work to automate the migration of our old computers to new computers. It's a little complicated how we are going to do it but basically we would like to deliver the new computer to the employee and have it automatically connect to our migration system. We are using PCmover to do the migration. I want this script to automate migration on the new computer.

We are going to be hiding PCmover behind another full-screen window that will be showing a progress bar. It also controls prepping the computer for migration and then closing the migration when it is complete (it was created in VB). We are going to be locking the keyboard and mouse while the migration is taking place. Because of this, I want the script to check on the program and make sure it is running. I've created a function that checks to see if PCmover is running and also checks if it is the active window. If the program is not running or if it is not the active window it will write and error to the log and then close the script and PCmover if needed. However, when I use @SW_SHOWMINIMIZED to hide it these two checks no longer work because PCmover isn't running in the process list and it is no longer an active window. Is there some other way I can check on the program to be sure it is running and if it isn't to close the script? Below is my script. Any help would be greatly appreciated!

#Include <WinAPI.au3>; for error checking

$lastError = ""; Initiate error log
$terminate = False; Intiate terminate
$endProcess = ""; Intiate end process
$parameter = ""; Intiate parameter input from user

; Create log file
$logFile = FileOpen("C:\PCmoverLog.txt",1)
FileWriteLine($logFile, "******************** NEW MIGRATION STARTED " & datetime() & " ********************")

; Check to see if keyboard and mouse need to be disabled
$flag = 0

If $CmdLineRaw = "locked" Then
    $flag = 1
ElseIf $CmdLineRaw = "unlocked" Then
    $flag = 0
ElseIf $CmdLineRaw = "" Then
    $flag = 1
EndIf

; Write to log the status of the keyboard and mouse
$paramter = $CmdLineRaw
If $parameter <> "" Then
    FileWriteLine($logFile, datetime() & " -- Keyboard and mouse are " & $parameter)
Else
    FileWriteLine($logFile, datetime() & " -- Parameter not specified.  Keyboard and mouse are locked")
EndIf

; Block/Unblock keyboard and mouse
BlockInput($flag)

; Start PCmover from Iron server
Run("\\iron\pc mover\pcmover.exe")
FileWriteLine($logFile, datetime() & " -- PCmover has started")

; Click Next on the first window
WinWait("PCmover", "Welcome to PCmover")
Send("!n")
FileWriteLine($logFile, datetime() & " -- Welcome window has been shown")

; Check for errors
checkErrors()

; Click the "I understand..." checkmark and then click Next
WinWait("PCmover", "IMPORTANT INFORMATION")
FileWriteLine($logFile, datetime() & " -- Important information window has been shown")
Send("!i")
FileWriteLine($logFile, datetime() & " -- I understand... checkmark has been checked")
WinWait("PCmover", "IMPORTANT INFORMATION")
Send("!n")

; Check for errors
checkErrors()

; Click "New Computer" and then click Next
WinWait("PCmover", "Preparing Your Computers for Migration")
FileWriteLine($logFile, datetime() & " -- Preparing your computers for migration window has been shown")
Send("!w")
FileWriteLine($logFile, datetime() & " -- New computer has been selected")
WinWait("PCmover", "Preparing Your Computers for Migration")
Send("!n")

; Check for errors
checkErrors()

; Click "Over a Network" and then click Next
WinWait("PCmover", "Method of Migration")
FileWriteLine($logFile, datetime() & " -- Method of migration window has been shown")
Send("!w")
FileWriteLine($logFile, datetime() & " -- Over a network has been selected")
WinWait("PCmover", "Method of Migration")
Send("!n")

; Check for errors
checkErrors()

; Click Next to search for applications
WinWait("PCmover", "&Next")
FileWriteLine($logFile, datetime() & " -- Search for applications window has been shown")
Send("!n")
FileWriteLine($logFile, datetime() & " -- Searching for applications...")

; Check for errors
checkErrors()

; Click Next to start transfer
WinWait("PCmover", "Ready to Start - Network Transfer")
FileWriteLine($logFile, datetime() & " -- Ready to start network transfer has been shown")
Send("!n")
FileWriteLine($logFile, datetime() & " -- Transferring data...")

; Check for errors
checkErrors()

; Uncheck "Reboot" and click Finish
WinWait("PCmover","Done")
FileWriteLine($logFile, datetime() & " -- Finished message has been shown")
Send("{SPACE}")
FileWriteLine($logFile, datetime() & " -- Reboot automatically has been deselected")
Send("{ENTER}")
FileWriteLine($logFile, datetime() & " -- The finish button has been clicked")

; Check for errors
checkErrors()

; Enable mouse and keyboard
BlockInput(0)

;Quit script
endScript()

; Function to find date and time
Func datetime() 
   $sec=string(@SEC)
   $min=string(@MIN) 
   $hour=@HOUR 
   $day=string(@MDAY) 
   $mon=string(@MON) 
   $year=string(@YEAR) 
   If $hour=00 then 
      $hour="24" 
   Else 
      $hour=string(@HOUR) 
   Endif 
   $time = $hour & ":" & $min & ":" & $sec
   $date= $mon & "-" & $day & "-" & $year 
   $dt=$date & " (" & $time & ")"
   Return ($dt) 
EndFunc

; Function to check for errors
Func checkErrors()
    $tick=5
    $counter=1
    While $counter <= $tick
        process()
        error()
        activeWindow()
        endScript()
        $counter = $counter + 1
        Sleep(300)
    WEnd
EndFunc

; Function to check if process is running
Func process()
    $path="\\Iron\PC mover\PCmover.exe"
    If Not FileExists($path) Then Exit
        $executable=StringRegExp($path, ".*\\(.*\.exe)",1)
    If @error Then Exit
        $executable=$executable[0] 
    If Not ProcessExists($executable) Then
        FileWriteLine($logFile, datetime() & " -- ERROR:" & $executable & " is not running")
        $terminate = True
    EndIf
    $endProcess = $executable
    Return ($terminate)
    Return ($endProcess)
EndFunc

; Function to write last error to log
Func error()
    $error=_WinAPI_GetLastErrorMessage()
    $errorNumber=_WinAPI_GetLastError()
    If $errorNumber <> 0 Then
        If $lastError <> $errorNumber Then
            FileWriteLine($logFile, datetime() & " -- ERROR:" & $error)
        EndIf
    EndIf
    $lastError = $errorNumber
    Return ($lastError)
EndFunc

; Function to check if PCmover is the active window
Func activeWindow()
    $activeWindow=WinGetTitle( "[ACTIVE]" )
    If $activeWindow <> "PCmover" Then
        FileWriteLine($logFile, datetime() & " -- ERROR:PCmover is not the active window")
        FileWriteLine($logFile, datetime() & " -- ERROR:" & $activeWindow & " is the active window")
        $terminate = True
        ProcessClose($endProcess)
        FileWriteLine($logFile, datetime() & " -- PCMOVER HAS BEEN CLOSED")
    EndIf
    Return ($terminate)
    Return ($endProcess)
EndFunc

; Function to terminate script
Func endScript()
    If $terminate = True Then
        FileWriteLine($logFile, datetime() & " -- SCRIPT HAS BEEN TERMINATED")
        Exit
    EndIf
    Return ($terminate)
EndFunc
Link to comment
Share on other sites

@SW_SHOWMINIMIZED Should not effect if a process is running are you sure somthing is not closing PCmover? Have you debugged?

PS. will look deeper in to code in a bit

I went back to my original script that just runs PCmover hidden without having any error checking. I ran it as a script and as compiled. I opened Windows Task Manager and watched as I ran both of them. As a script, a process called AutoIt3.exe runs and as a compiled file the filename is running (compliled as startpcmover.exe). Neither of them had a pcmover.exe running. However, I know from my log file that PCmover is in fact running because it is walking through the migration steps.

The interesting thing is that when I remove the @SW_SHOWMINIMIZED from the code, there is a pcmover.exe process running. Something is hiding the pcmover.exe process whenever I hide the program from view.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...