# Need to make a script that chooses a random time then creates scheduled task

## Recommended Posts

I am working on a power saving strategy for my company to standby all our computers at 6pm. That part isn't hard. The trick is waking them all back up. I could create a simple scheduled task to run at 6am and have them all wake up, but I am concerned our power systems wouldn't handle thousands of computers all turning on at the exact same time in our company.

So, I have thought to create a script that would run during the runonce (we are reimaging all our systems soon) that would basically do the following.

Choose any minute between 6am and 6:10am.

Once it has chosen a time create a scheduled task to run a certain exe and set it to "wake this computer to run task" and set the time to run as the random time from above.

##### Share on other sites

Ok, well there isn't a function in autoit as far as i know to grab the time, but there is this: @HOUR. This will return the hour of the computer clock in 24 hour format (00 - 23).

So maybe something like this:

```While 1

If @HOUR = 06 Then

\$minute = Random(0, 10, 1)
EndIf

If @TIME = 06 And \$minute = @MIN Then

Run(blah)

EndIf
WEnd```

Of course, this is just a rough outline code of course, but maybe it will help.

##### Share on other sites

Try this one

```\$Minute = Random (0, 10, 1)
RunWait (@ComSpec & " /c " & "at 6:" & \$Minute & " app.exe")```

It would do the trick for creating a random scheduled task

##### Share on other sites

I've got the code I need for the scheduled task, I just need to set a random time between 6am and 6:10am as a variable.

Setting it as a var is easy enough, but choosing a random time between that 10 minute period is tricky.

Can you help me with a between statement. IE

If number is greater than 0 but less than 5 then do something

else if number is greater than 5 but less than 10 do something

I'm having trouble with my expression and operators for doing things if a number is between 2 values.

Here is my code, but it's not working.

```If \$random1 <= 100 Then
msgbox(0, "title", "0 and 100, " & \$random1)
ElseIf \$random1 <= 101 And \$random1 >= 200 Then
msgbox(0, "title", "100 and 200 " & \$random1)
Else
msgbox(0, "title", "else "  & \$random1)
EndIf```
Edited by kor

##### Share on other sites

Well, it seems XP doesnt support creating a task via command line that will allow me to set the "wake this computer up to run task" switch.

A workaround I have found on the internet is to create task with that switch already enabled, then copy it into the scheduled tasks after the fact and change what you want, however XP also does not support changing the start time via the schtasks /change command. So I'm SOL on scripting it.

So now I'm wondering if anyone knows how I might go about creating a scheduled tasks with the options that I want to specify the old fashioned way of scripting manual mouse clicks and button presses.. etc. It's an ugly way of doing it, but it's my last option.

I need to open control panel

create a new scheduled task and fill out various information on all the tabs. Can someone point me in the right direction on how to code that stuff?

##### Share on other sites

I need to open control panel

create a new scheduled task and fill out various information on all the tabs. Can someone point me in the right direction on how to code that stuff?

##### Share on other sites

Here is a revised version. I have cleaned up some stuff and broken it into functions. I hope this makes it easier to track what the script is doing. I have also added code to make this run as silently as possible. Obviously there is a great deal more that could be added to this script. Please read all of the comments I have included in the script, it will save you alot of time troubleshooting if you do.

I hope this gets you on the right track.

Dan Chancellor

```#comments-start

Added extra logic, broke it up into functions.
Removed some unused variables.
Added as near silent running as I could figure out.
The end user will still see a few blips here and there.

**************************************************************************
NOTE - Very Important - Set the values of the follwoing variables to match

Found in the main body near the top -
\$_NewTaskName = "MyNewTask"    ; Change This to the name of the job you wish to create

Found in the _EditTask Function near the top -
\$_DomainUserName = "domain\user" ; *** - Change this to a valid user on your systems
\$_TargetProgram = "c:\program files\auitscripts\myscript.exe" ; Change this to the name of
the program you want to run
**************************************************************************

AutoItSetOption("WinTitleMatchMode",2) ; Set WinTitleMatchMode to match any substring of the title
AutoItSetOption("WinDetectHiddenText",1) ; Set to detect text on Hidden Windows
; Note the AutoItSetOption("SendKeyDelay",xxx) may need to be tuned for your situation
AutoItSetOption("SendKeyDelay",50) ; Set delay of 500 miliseconds between keys for sendkey operations

Local \$_NewTaskName = "MyNewTask"   ; Change This to the name of the job you wish to create

;That's all folks!

Exit(@AutoItPID)

; Function Library

; run the scheduled tasks folder hidden

Local \$_WinHandle = ""
\$_PID = Run("explorer.exe " & @WindowsDir & "\Tasks","",@SW_HIDE)
; Wait for the Scheduled Tasks Folder to be active
\$_WinState = 0
While \$_WinState = 0
WEnd
; get winhandle and use for all send operations

Return \$_WinHandle

Else
EndIf

EndFunc ;   _ChkForExistingJob()

; probably should turn off keyboard and mouse input during create / edit functions
; keep in mind, if the script fails, the keyboard and mouse will still be disabled.
; Usually, you can get control back by using Ctrl-Alt-Del.

; Definately comment out the next line during development and testing
BlockInput(1) ; be sure to toggle this off just before the "return" statement

;
; NOTE - If you try to bundle too many keystrokes/functionality together
; things probably will not work as expected, there is a reason these are
; broken into multiple ControlSend statements with only a few functions per statement.
;
; press the enter key to save the new name
; Rename task to desired job name
; Refresh Scheduled Tasks Window after renaming the file

EndIf

BlockInput(0)

Return

; *** - Change this to a valid user on your systems

; *** - Change this to the password for the user on your systems

; Change to the program you want to run
\$_TargetProgram = "c:\program files\auitscripts\myscript.exe"

; The keyboard and mouse input should be disabled during create / edit functions
; keep in mind, if the script fails, the keyboard and mouse will still be disabled.
; Usually, you can get control back by using Ctrl-Alt-Del.

; Definately comment out the next line during development and testing
BlockInput(1) ; be sure to toggle this off just before the "return" statement

;
; NOTE - If you try to bundle too many keystrokes/functionality together
; things probably will not work as expected, there is a reason these are
; broken into multiple ControlSend statements with only a few functions per statement.
;

; type in name of the new task to locate and highlight/select in Scheduled Tasks Window
; Press Enter to open the edit properties of the task

; Re-Set WinTitleMatchMode to match from Left to Right
AutoItSetOption("WinTitleMatchMode",1)

; Now get the Winhandle for the Task Properties Window
\$_WinState = 0
While \$_WinState = 0
WEnd
WinSetState(\$_WinhandleProperties,"",@SW_HIDE)

; Set Run As User and Password
; Select the "Run as: field
ControlSend(\$_WinhandleProperties,"","","!u")
; Select the Set Password function
ControlSend(\$_WinhandleProperties,"","","!s")

; Get Winhandle for the password dialog box
\$_WinState = 0
While \$_WinState = 0
WEnd
; Hide the password dialog box
WinSetState(\$_WinhandlePWD,"",@SW_HIDE)

; Tab to second password entry
ControlSend(\$_WinhandlePWD,"","","{Tab}")
; Close / Finish password function
ControlSend(\$_WinhandlePWD,"","","{Enter}")

; Alt+r This will take you to the Run: field
ControlSend(\$_WinhandleProperties,"","","!r")
; Enter command line of command this task should run
ControlSend(\$_WinhandleProperties,"","",\$_TargetProgram)

;Send Shift Tab to get to the various "tabs"
; This will only work when starting from the Run: field
ControlSend(\$_WinhandleProperties,"","","+{Tab}")
; This will take you to the Settings Tab
ControlSend(\$_WinhandleProperties,"","","{RIGHT}{RIGHT}")

;Change the power Management Settings
; toggle the "running on battery" Power Management Settings
ControlSend(\$_WinhandleProperties,"","","!b")
; Toggle the "battery mode" Power Management Settings
ControlSend(\$_WinhandleProperties,"","","!y")
; toggle the "wake the computer" Power Management Settings
ControlSend(\$_WinhandleProperties,"","","!w")
; close the Task Properties Window
ControlSend(\$_WinhandleProperties,"","","{ENTER}")

; Close the windows if they are still running
If WinExists(\$_WinhandleProperties) Then  WinClose(\$_WinhandleProperties)

BlockInput(0)

Return

## Create an account

Register a new account

×

• Wiki

• Back

• Git