Jump to content

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


kor
 Share

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.

Link to comment
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.

Link to comment
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
Link to comment
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

goto scheduled tasks

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?

Link to comment
Share on other sites

  • 3 months later...

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
 the job and your environment;
 
     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
     $_UserPassword = "password" ; *** - Change this to the password for the user on your systems
     $_TargetProgram = "c:\program files\auitscripts\myscript.exe" ; Change this to the name of 
     the program you want to run
 **************************************************************************
 
#comments-end

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 $_TaskExists = True
Local $_NewTaskName = "MyNewTask"   ; Change This to the name of the job you wish to create
Global $_SchTaskWinHandle

$_SchTaskWinHandle = _StartScheduledTasks()
$_TaskExists = _ChkForExistingJob($_NewTaskName)
If $_TaskExists = False Then _CreateNewTask($_NewTaskName)
_EditTask($_NewTaskName)

;That's all folks!

Exit(@AutoItPID)


; Function Library


Func _StartScheduledTasks()
    
    ; 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
        $_WinState = WinGetState("Tasks","FolderView")
    WEnd
    ; get winhandle and use for all send operations
    $_WinHandle = WinGetHandle("Tasks","FolderView")

    Return $_WinHandle
    
EndFunc ;   _StartScheduledTasks()


Func _ChkForExistingJob(ByRef $_Task)
    
    If FileExists(@WindowsDir & "\Tasks\" & $_Task & ".job") Then
        $_TaskExists = True
    Else
        $_TaskExists = False
    EndIf
    
    Return $_TaskExists
    
EndFunc ;   _ChkForExistingJob()


Func _CreateNewTask(ByRef $_Task)
    
    ; 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

    ;Create the new task
    If  $_SchTaskWinHandle Then
        ;
        ; 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.
        ;
        ; UsE the menu bar File menu item to create a new task
        ControlSend($_SchTaskWinHandle,"","","!fws")
        ; press the enter key to save the new name
        ControlSend($_SchTaskWinHandle,"","","{ENTER}")
        ; Rename task to desired job name
        If FileExists(@WindowsDir & "\Tasks\New Task.job") Then _
            FileMove(@WindowsDir & "\Tasks\New Task.job",@WindowsDir & "\Tasks\" & $_Task & ".job" )
        ; Refresh Scheduled Tasks Window after renaming the file
        ControlSend($_SchTaskWinHandle,"","","{F5}")
        
    EndIf
    
    BlockInput(0)
    
    Return
    
EndFunc ;    _CreateNewTask(ByRef $_Task)


Func _EditTask(ByRef $_Task)
    
    ; *** - Change this to a valid user on your systems
    $_DomainUserName = "domain\user"
    
    ; *** - Change this to the password for the user on your systems
    $_UserPassword = "password"
    
    ; 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
    ControlSend($_SchTaskWinHandle,"","",$_Task)
    ControlSend($_SchTaskWinHandle,"","","{ENTER}")
    
    ; 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
        $_WinState = WinGetState($_Task,"Task")
    WEnd
    $_WinhandleProperties = WinGetHandle($_Task,"Task")
    WinSetState($_WinhandleProperties,"",@SW_HIDE)
    
    ; Set Run As User and Password
    ; Select the "Run as: field
    ControlSend($_WinhandleProperties,"","","!u") 
    ; Set the Domain\UserName
    ControlSend($_WinhandleProperties,"","",$_DomainUserName) 
    ; Select the Set Password function
    ControlSend($_WinhandleProperties,"","","!s")

    ; Get Winhandle for the password dialog box
    $_WinState = 0
    While $_WinState = 0
        $_WinState = WinGetState("Set Password","&Password:")
    WEnd
    $_WinhandlePWD = WinGetHandle("Set Password","&Password:")
    ; Hide the password dialog box
    WinSetState($_WinhandlePWD,"",@SW_HIDE)
    
    ; Set first password entry for Domain\UserName
    ControlSend($_WinhandlePWD,"","",$_UserPassword)
    ; Tab to second password entry
    ControlSend($_WinhandlePWD,"","","{Tab}")
    ; Set second password entry for Domain\UserName
    ControlSend($_WinhandlePWD,"","",$_UserPassword)
    ; 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)
    If WinExists($_SchTaskWinHandle) Then WinClose($_SchTaskWinHandle)
    
    BlockInput(0)
    
    Return
    
EndFunc ;   _EditTask($_NewTaskName)
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...