Sign in to follow this  
Followers 0
Stein

Remote start scheduled task and wait till it completes

1 post in this topic

#1 ·  Posted (edited)

Hello all,

First off let me give you some background on the overall process, I'm currently in the process of automating manual tasks we have to do on a few of our servers. These tasks involve running a command line and waiting for it to finish and getting the customer number output it gives us. Once we finish the command line process we have scheduled tasks in place on the servers that we then would manually run and wait for them to complete.

The first part of my script (The command line & output) I've been able to automate fine. I actually used one of the GUI's posted on this forum and modified it to fit my needs. The second part of my script (running the scheduled jobs) I'm having a bit of an issue dealing or I really should say, coming up with the most efficient way to get the result I want. Right now although my code works and starts the scheduled task it's alerting me on when the task started rather when it actually completes (sometimes it can take up to 30 to 45 minutes for these scheduled tasks to complete).

Below I've included some of my example code, keep in mind each of my GUI buttons I'm simply calling upon a function...

;;;;;;;;;;;Test Functions;;;;;;;;;;;
Func _Test() ;this is the function that runs the command line command (real command is an HP Exstream command, I used ipconfig as an example for testing)
SplashTextOn("Task Is Running", "The task is currently running. Please wait till the Task Completed message box with the output appears.", -1, 75)
$PID=RunAsWait($Username, $Domain, $Password, 2, @ComSpec & " /c " & "psexec \\remotemachine -u domain\adminaccount -p passwordgoeshere ipconfig > Test.log", "C:\Tools\", @SW_HIDE)
ProcessClose ($PID)
SplashOff()
If $PID = 0 or 4 Then
    MsgBox(0,"Task Completed", _TestArray())
ElseIf $PID <> 0 or 4 Then
    MsgBox(16, "Error", "The command you were trying to run failed, please contact a System Administrator.")
EndIf
EndFunc ;==>_Test

Func _TestJob() ;this is the function that runs the scheduled task
SplashTextOn("Task Is Running", "The task is currently running. Please wait till the Task Completed message box with the output appears.", -1, 75)
$PID=RunAsWait($Username, $Domain, $Password, 2, 'psexec \\remotemachine -u domain\adminaccount -p passwordgoeshere schtasks /Change /TN "TestTask" /Enable', "C:\Tools\", @SW_HIDE)
$PID=RunAsWait($Username, $Domain, $Password, 2, 'psexec \\remotemachine -u domain\adminaccount -p passwordgoeshere schtasks /Run /TN "TestTask"', "C:\Tools\", @SW_HIDE)
ProcessClose ($PID)
SplashOff
If $PID = 0 Then
    MsgBox(0,"Task Completed", "The manual run of the scheduled job completed successfully!")
    RunAsWait($Username, $Domain, $Password, 2, 'psexec \\remotemachine -u domain\adminaccount -p passwordgoeshere schtasks /Change /TN "TestTask" /Disable', "C:\Tools\", @SW_HIDE)
ElseIf $PID <> 0 Then
    MsgBox(16, "Error", "The command you were trying to run failed, please contact a System Administrator.")
EndIf
EndFunc ;==>_TestJob
;;;;;;;;;;;End Test Functions;;;;;;;;;;;

There you have it, I'm using psexec to do the majority of my automation and I have to use RunAsWait because I'm creating a tool that only a certain department will have access to and they don't have the elevated rights to run psexec on these servers.

Also just because it's referenced in the code above the function I call for the message box:  MsgBox(0,"Task Completed", _TestArray()) is the below function.

Func _TestArray() ; Read into an aray and then just extrat the line we want
    $iBegin = TimerInit()
    $iCount = _FileCountLines($LFileT)
    $lastSline = FileReadLine($LFileT, $iCount - 1)
    ConsoleWrite(TimerDiff($iBegin) & @CRLF)
    Return $lastSline
EndFunc

So far the only ideas I have to get the true result of when the scheduled task finishes is to leverage the scheduled tasks query command as follows schtasks /query /fo list /tn "TaskNameHere" > logname.log

and then loop search the log file until the task doesn't show a status of running.

Any ideas or thoughts any of you may have or possibly even some example code would go a long way in helping me finally complete this lengthy automation script I've been working on. Our goal is to get these tasks off the responsibility of the systems team and allow the department that's always requesting the tasks to run them directly without actually having to delegate them permissions on the servers. Also this is the first real AutoIt script I've ever wrote besides a few test example scripts. I've been using this project as a learning experience for scripting in AutoIt.

Edited by Stein

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