Jump to content

Scheduled Task - Script Won't Finish


Recommended Posts

First the Situation:

I'm trying to automate an image uploading utility which lives on the client side of some cloud based server/client software and which was not designed to be automated. I've got a 2012 R2 VM running with autologon enabled on a local account.  The account with autologon enabled has a scheduled task set up to run at 4AM daily.  This task merely runs my AutoIt script.

Now the issue:

If I remote into my VM and run the script manually, it runs perfectly. If I edit my scheduled task to run only a few minutes in the future, and then reboot the VM, it also works perfectly. If I leave it to run overnight, the script starts but never finishes. It seems to be getting hung up waiting for the first window to become active. I've tried many version of the script now with no luck.  The latest version is below.

#include <File.au3>

Dim $un = "username"
Dim $pw = "password"

; Login window
Run("C:\Program Files (x86)\CSI Software\Spectrum NG\Spectrum NG Client\SNGImageUploader.exe")
Dim $hLogin = WinWait("[CLASS:WindowsForms10.Window.8.app.0.2bf8098_r11_ad1]", "Version: ")
SendKeepActive($hLogin)
WinActivate($hLogin)
Send($un & "{TAB}" & $pw & "{ENTER}")
SendKeepActive("")

; Main window - 1/3
Dim $hMain = WinWaitActive("SNG Image Uploader")
SendKeepActive($hMain)
Send("{TAB}{ENTER}")
SendKeepActive("")

; File browser window
Dim $hBrowse = WinWaitActive("Browse For Folder")
SendKeepActive($hBrowse)
Send("{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{RIGHT}{DOWN}")
Sleep(2000)  ; Give the program moment to detect images in the directory
Send("{ENTER}")
SendKeepActive("")

; Main window - 2/3
SendKeepActive($hMain)
WinWaitActive($hMain)
Send("{TAB}{ENTER}")
SendKeepActive("")

; Upload complete window
Dim $hUpload = WinWaitActive("[CLASS:#32770]", "Upload complete. Please wait while servers process your request.")
SendKeepActive($hUpload)
Send("{ENTER}")
SendKeepActive("")

; Import complete window
Dim $hImport = WinWaitActive("[CLASS:#32770]", "Image import completed. Please check results in the Account Upload History screen.")
SendKeepActive($hImport)
Send("{ENTER}")
SendKeepActive("")

; Main window - 3/3
WinWaitActive($hMain)
WinClose($hMain)

; Cleanup
FileDelete("C:\CRPics\*")

 

Link to comment
Share on other sites

I've added extensive logging to my script.  Here's the new version:

#include <File.au3>

Dim $un = "username"
Dim $pw = "password"

Dim $flog = FileOpen("C:\Users\CRPics\Desktop\SendCRPics.log", 1);
_FileWriteLog($flog, "")

; Login window
_FileWriteLog($flog, "Running SNGImageUploader")
Run("C:\Program Files (x86)\CSI Software\Spectrum NG\Spectrum NG Client\SNGImageUploader.exe")
_FileWriteLog($flog, "Waiting for the login window to exist")
Dim $hLogin = WinWait("[CLASS:WindowsForms10.Window.8.app.0.2bf8098_r11_ad1]", "Version: ")
_FileWriteLog($flog, "Attempting to keep the login window active")
SendKeepActive($hLogin)
_FileWriteLog($flog, "Activating the login window")
WinActivate($hLogin)
_FileWriteLog($flog, "Logging in")
Send($un & "{TAB}" & $pw & "{ENTER}")
_FileWriteLog($flog, "Clearing SendKeepActive state")
SendKeepActive("")

; Main window - 1/3
_FileWriteLog($flog, "Waiting for the main window to become active")
Dim $hMain = WinWaitActive("SNG Image Uploader")
_FileWriteLog($flog, "Attempting to keep the main window active")
SendKeepActive($hMain)
_FileWriteLog($flog, "Clicking the browse button")
Send("{TAB}{ENTER}")
_FileWriteLog($flog, "Clearing the SendKeepActive state")
SendKeepActive("")

; File browser window
_FileWriteLog($flog, "Waiting for the file browser window to become active")
Dim $hBrowse = WinWaitActive("Browse For Folder")
_FileWriteLog($flog, "Attempting to keep the file browser window active")
SendKeepActive($hBrowse)
_FileWriteLog($flog, "Browsing to the image directory")
Send("{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{RIGHT}{DOWN}")
_FileWriteLog($flog, "Waiting for the program to read the image directory")
Sleep(2000)  ; Give the program moment to detect images in the directory
_FileWriteLog($flog, "Choosing the directory")
Send("{ENTER}")
_FileWriteLog($flog, "Clearing the SendKeepActive state")
SendKeepActive("")

; Main window - 2/3
_FileWriteLog($flog, "Attempting to keep the main window active")
SendKeepActive($hMain)
_FileWriteLog($flog, "Waiting for the main window to become active")
WinWaitActive($hMain)
_FileWriteLog($flog, "Clicking the upload button")
Send("{TAB}{ENTER}")
_FileWriteLog($flog, "Clearing the SendKeepActive state")
SendKeepActive("")

; Upload complete window
_FileWriteLog($flog, "Waiting for the 'upload complete' dialog to become active")
Dim $hUpload = WinWaitActive("[CLASS:#32770]", "Upload complete. Please wait while servers process your request.")
_FileWriteLog($flog, "Attempting to keep the 'upload complete' dialog active")
SendKeepActive($hUpload)
_FileWriteLog($flog, "Clicking OK")
Send("{ENTER}")
_FileWriteLog($flog, "Clearing the SendKeepActive state")
SendKeepActive("")

; Import complete window
_FileWriteLog($flog, "Waiting for the 'import complete' dialog to become active")
Dim $hImport = WinWaitActive("[CLASS:#32770]", "Image import completed. Please check results in the Account Upload History screen.")
_FileWriteLog($flog, "Attempting to keep the 'import complete' dialog active")
SendKeepActive($hImport)
_FileWriteLog($flog, "Clicking OK")
Send("{ENTER}")
_FileWriteLog($flog, "Clearing the SendKeepActive state")
SendKeepActive("")

; Main window - 3/3
_FileWriteLog($flog, "Waiting for the main window to become active")
WinWaitActive($hMain)
_FileWriteLog($flog, "Closing the program")
WinClose($hMain)

; Cleanup
_FileWriteLog($flog, "Deleting images from the image directory")
FileDelete("C:\CRPics\*")

FileClose($flog);

 

Link to comment
Share on other sites

  • Developers

Maybe it helps when you also share the result of your logging? ;)

In general: Send() will not work when ran with the scheduler so you need to find a way to use CtrlSend()

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Odd that it works when I schedule it for just a few minutes into the future then.  I'll post tomorrow when I get the log results.  I tried using ControlSend() in at least one version of the script, but I couldn't get it to work for the username/password boxes on the login screen.  Granted I'm a total novice, so I'll try again if I don't figure something else out really quick after looking at the log in the morning.

Link to comment
Share on other sites

Oops - I forgot to restart the VM.  I left it logged into an RDP session all night.  The good news is it finished successfully, but it's not a good test of a typical run situation.  I played around a bit more with ControlSend() yesterday, and I think I'm getting it figured out, so I'm going to change the script to get rid of all the calls to Send().

Link to comment
Share on other sites

I got it working. The key was to switch to ControlSend() away from Send() as suggested. Thank you Jos. Here's the latest, hopefully final, version:

#include <File.au3>

Dim $un = "username"
Dim $pw = "password"

; Login window
Run("C:\Program Files (x86)\CSI Software\Spectrum NG\Spectrum NG Client\SNGImageUploader.exe")
Dim $hLogin = WinWait("[CLASS:WindowsForms10.Window.8.app.0.2bf8098_r11_ad1]", "Version: ")
ControlSend($hLogin, "", "[CLASS:WindowsForms10.EDIT.app.0.2bf8098_r11_ad1; INSTANCE:1]", $un & "{TAB}")
Sleep(1000)
ControlSend($hLogin, "", "[CLASS:WindowsForms10.EDIT.app.0.2bf8098_r11_ad1; INSTANCE:1]", $pw)
Sleep(1000)
ControlClick($hLogin, "", "[CLASS:WindowsForms10.Window.8.app.0.2bf8098_r11_ad1; INSTANCE:4]")

; Main window - 1/3
Dim $hMain = WinWait("SNG Image Uploader")
ControlClick($hMain, "", "[CLASS:WindowsForms10.BUTTON.app.0.2bf8098_r11_ad1; INSTANCE:6]")

; File browser window
Dim $hBrowse = WinWait("Browse For Folder")
Sleep(10000)
ControlSend($hBrowse, "", "[CLASS:SysTreeView32; INSTANCE:1]", "{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{RIGHT}{DOWN}")
Sleep(2000)  ; Wait for the program to read the image directory
ControlClick($hBrowse, "", "[CLASS:Button; INSTANCE:2]")
Sleep(2000)

; Main window - 2/3
ControlClick($hMain, "", "[CLASS:WindowsForms10.BUTTON.app.0.2bf8098_r11_ad1; INSTANCE:5]")

; Upload complete window
Dim $hUpload = WinWait("[CLASS:#32770]", "Upload complete. Please wait while servers process your request.")
ControlClick($hUpload, "", "[CLASS:Button; INSTANCE:1]")

; Import complete window
Dim $hImport = WinWait("[CLASS:#32770]", "Image import completed. Please check results in the Account Upload History screen.")
ControlClick($hImport, "", "[CLASS:Button; INSTANCE:1]")

; Main window - 3/3
WinClose($hMain)

; Cleanup
FileDelete("C:\CRPics\*")

 

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