• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

How to schedule task with random time via command line

Hi all,

We schedule tasks via the command line on hundreds of machines to perform some automated tasks daily. Unfortunately, because the schedule is ran at the same time on every machine, it can cause issues with resources, bandwidth, etc. e.g. hundreds of machines trying to connect to the same server to obtain large files.

What I would like to be able to do is reschedule a task at a random time, so that each machine will not connect at the exact same time. I want to be able to utilize Windows NT and Windows 2000 scheduling services that are already on the machine. So some how, passing a random time into the command line or something.

Any suggestions would be greatly appreciated.
0
mpaull
Asked:
mpaull
  • 3
  • 2
1 Solution
 
canaliCommented:
This the script to create a job at random time
save it as randomTaskcreate.vbs

cscript remoteComputer  "C:\Documents and Settings\G\Desktop\explore2fs-1.08beta7.exe"
or
cscript remoteComputer  notepad.exe

example create job on local computer a notepad job at random time:
cscript . notepad.exe

Gastone

'http://www.microsoft.com/technet/scriptcenter/guide/sas_man_lpja.mspx?mfr=true
' randomTaskcreate.vbs
'gastone canali
option explicit
Dim strComputer,randomNumber,h,m,jobTime,objService,objNewJob,JobID,errJobCreated
sub createRNDjob (computer,time,job)
    strComputer = computer
    jobtime="********" & time & "00.000000-420"
    Set objService = GetObject("winmgmts:\\" & strComputer)
    Set objNewJob = objService.Get("Win32_ScheduledJob")
    ' every day 127
   '  1 OR 4 OR 16  MONDAY, WEDNESDAY, and FRIDAY
    errJobCreated = objNewJob.Create (job, jobtime, True ,127, , , JobID)
    If Err.Number = 0 Then
      Wscript.Echo "New Job ID: " & JobID
    Else
     Wscript.Echo "An error occurred: " & errJobCreated
    End If
    set objService=Nothing
    Set objNewJob=Nothing
end sub
Function rand (max)
   randomize()
   randomNumber=Int(max * rnd())
   rand=randomNumber
End Function
function RandTime
   h=cstr(rand (24))
   if (len(h)<2) then h="0"& h
   m=cstr(rand (60))
   if (len(m)<2) then m="0"& m
   RandTime=h & m
end function

Dim oArgs,Counter
Set oArgs = WScript.Arguments
Counter = oArgs.Count
If (Counter <> 2) Then
   WScript.echo "Wrong parameters"
   WScript.echo "Usage:  cscript randomTaskcreate.vbs computer job"
   'WScript.Quit 1
else
  WScript.echo "---------"
  createRNDjob  oArgs.item(0),RandTime,oArgs.item(1)
end if
set oArgs=Nothing
0
 
mpaullAuthor Commented:
Thanks Gastone, that works! Just one more question; is there a way that the script can be changed so that you can pass in a band of time of which to schedule a random time schedule in?

e.g. cscript rts.vbs localhost 0000 0200 "c:\mybatchfile.bat"

The script could then schedule the mybatchfile.bat to run any time between 12am and 2 am.

Regards, Charlie
0
 
canaliCommented:
Quick solution
change in  h=cstr(rand (24)) with 2

'modified for generation from 0000 to 0159
function RandTime
   h=cstr(rand (2))
   if (len(h)<2) then h="0"& h
   m=cstr(rand (60))
   if (len(m)<2) then m="0"& m
   RandTime=h & m
end function

If need the script with new params tell me
 Gastone
0
 
mpaullAuthor Commented:
Hi Gastone,

If I understand this correctly, I am changing a hardcoded value inside the script. Is there a way to pass values into the script from the command line, just like I am passing in the computers name and the program to run. In my above example:

e.g. cscript rts.vbs localhost <start time> <end time> "c:\mybatchfile.bat"

where start and end time are of which the script will schedule the mybatchfile.bat a random time.

e.g. cscript rts.vbs localhost 0400 0500 "c:\mybatchfile.bat" would schedule the mybatchfile.bat to run between the hours of 4am and 5am.

Regards, Charlie
0
 
canaliCommented:
The final script
u can decide the hours not minutes (pacific time, here in italy rts.vbs don't work correctly ;)
 cscript rts.vbs localhost 4 5 "c:\mybatchfile.bat" would schedule the mybatchfile.bat to run between the hours of 4am and 4:59am.

I added exact time scheduling, in this case u can use also minutes  (only when starttme>endtime  145>0 )
 cscript rts.vbs localhost 0145 0 "c:\mybatchfile.bat" would schedule the mybatchfile.bat to run at 1:45am.

Bye Gastone


'http://www.microsoft.com/technet/scriptcenter/guide/sas_man_lpja.mspx?mfr=true
' randomTaskcreate.vbs
'cscript rts.vbs localhost 4 5 "c:\mybatchfile.bat"
'gastone canali
option explicit
Dim strComputer,randomNumber,h,m,jobTime,objService,objNewJob,JobID,errJobCreated
sub createRNDjob (computer,time,job)
    strComputer = computer
    jobtime="********" & time & "00.000000-420"
    Set objService = GetObject("winmgmts:\\" & strComputer)
    Set objNewJob = objService.Get("Win32_ScheduledJob")
    ' every day 127
   '  1 OR 4 OR 16  MONDAY, WEDNESDAY, and FRIDAY
    errJobCreated = objNewJob.Create (job, jobtime, True ,127, , , JobID)
    If Err.Number = 0 Then
      Wscript.Echo "New Job ID: " & JobID
    Else
     Wscript.Echo "An error occurred: " & errJobCreated
    End If
    set objService=Nothing
    Set objNewJob=Nothing
end sub
Function rand (max)
   randomize()
   randomNumber=Int(max * rnd())
   rand=randomNumber
End Function
function RandTime(startT,endT)
   h=cstr(rand (endT))+startT
   if (len(h)<2) then h="0"& h
   m=cstr(rand (60))
   if (len(m)<2) then m="0"& m
   RandTime=h & m
end function

Dim oArgs,Counter
Set oArgs = WScript.Arguments
Counter = oArgs.Count
WScript.echo Counter
If (Counter <> 4) Then
   WScript.echo "Wrong parameters"
   WScript.echo "Usage:  cscript randomTaskcreate.vbs computer startHOUR endHOUR job"
   WScript.echo "        startHOUR endHOUR  valid values only integer 0 1 2...24"
   WScript.echo "----------------------------------------------------------------"
   WScript.echo "        range from 00:00AM to 1:59AM"
   WScript.echo "Usage:  cscript randomTaskcreate.vbs localhost 0 2 notepad.exe"
   WScript.echo "----------------------------------------------------------------"
   WScript.echo "         range from 1:00AM to 1:59AM"
   WScript.echo "Usage:  cscript randomTaskcreate.vbs localhost 1 1 notepad.exe  "
   WScript.echo "----------------------------------------------------------------"
   WScript.echo "        create job at exact  time if StartHOUR>endHOUR"
   WScript.echo "        StartHOUR=exaxct time 01:23 AM"
   WScript.echo "Usage:  cscript randomTaskcreate.vbs localhost 0123 0 notepad.exe  "
   
   'WScript.Quit 1
else
  WScript.echo "---------"
  If oArgs.item(2)<oArgs.item(1) Then
        'job at exact time
        createRNDjob  oArgs.item(0),oArgs.item(1),oArgs.item(3)
  Else
        createRNDjob  oArgs.item(0),RandTime(oArgs.item(1),oArgs.item(2)),oArgs.item(3)
End If
end if
set oArgs=Nothing
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now