Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mass addition of scheduled tasks 2

Status
Not open for further replies.

RobertT687

Programmer
Apr 2, 2001
425
US
Here's the scenario.
My company has a need to create several scheduled tasks on a large number (100+) users PCs. I thought I could do this by creating a batch file (MyCommands.cmd) that executes several "AT" commands to create the scheduled tasks. That part works. However, I cannot edit the task because all properties are greyed out. What can I do to make this task editible?
This will be run on both Laptop and Desktop PC running Windows XP/SP2.

I welcome any suggestions.

Thanks in advance!
 
Anything here?

HOW TO: Work with Scheduled Tasks on Remote Computers in Windows XP

Schtasks
Schedules commands and programs to run periodically or at a specific time. Adds and removes tasks from the schedule,starts and stops tasks on demand, and displays and changes scheduled tasks.



Running a Task on Multiple Computers

 
linney

Thanks and have a star.

I'll give schtasks a try. Looks exactly like what I'm after.

I'll post back after I've tried it.
 
Here are 2 scripts I created. [borrowed a bit of Mark's one script for the date part]. Delete parts if you want.
Hope it helps.

Code:
Dim varToday, Verify, LastRunDate
Dim WshShell, WshEnv, strLocalfile
Dim newfolder, newfolderpath
Dim strCurrentFolder
Dim objFSO, I

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("Process")
SysDrive = WshEnv("%SYSTEMDRIVE%")
SysRoot = WshEnv("SYSTEMROOT")
LogonSvr = WshEnv("LOGONSERVER")

'Select Case True
'  Case  objfso.fileexists("cleanmgr.vbe") and not objfso.fileexists("cleanmgr.job")
      'Copy the cleanmgr.vbe file
'  Case Not objfso.fileexists("cleanmgr.vbe") and not objfso.fileexists("cleanmgr.job")
'  'copy the cleanmgr file and create the cleanmgr.job
'End Select
Const OverwriteExisting = True
'-------------------------------------------------------------------------------------------------------------------------
'Do not run on these machines (list of servers)
Set objNetwork = CreateObject("WScript.Network")
	thisComputer = objNetwork.ComputerName
	arrComputers = Array("server","server1")
For Each arrayElement in arrComputers
	if arrayElement = thisComputer then
		wscript.Quit
	end if 
next
'----------------------------------------------------------------------------------------------------------------------------
'Check if this is a Server. If this is a server quit
'We don't want to delete any scheduled jobs on servers
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
  If InStr(1,objItem.Caption,"Server") Then Wscript.Quit
Next
'----------------------------------------------------------------------------------------------------------------------------
'Lets get rid of those old jobs
'Delete any jobs that may exist having been created using At
On Error Resume Next
  For I = 1 To 20
      objFSO.DeleteFile SysRoot & "\Tasks\At" & I & ".job"
  Next 
'----------------------------------------------------------------------------------------------------------------------------
'Now delete those registry keys of previous scheduled jobs
WshShell.RegDelete "HKLM\SOFTWARE\MyInstallsAndFixes\Defrag"
WshShell.RegDelete "HKLM\SOFTWARE\MyInstallsAndFixes\Stinger"
On Error goto 0
'----------------------------------------------------------------------------------------------------------------------------
'Now Create a registry value so if this is run it wont run again today
varToday = Cleanmgr
	varToday = Weekday(Date)

Verify = "HKLM\SOFTWARE\MyInstallsAndFixes\" 

 'Check if scan has run today and if so exit
On Error Resume Next
LastRunDate = WshShell.RegRead(Verify & "Cleanmgr")
If LastRunDate =  cstr(Date) Then
      WScript.Quit
Else 
     WshShell.RegWrite Verify & "Cleanmgr",Date,"REG_SZ"
End If
'----------------------------------------------------------------------------------------------------------------------------
'CreateNewFolder
newfolderpath = SysRoot & "\MyProgs"

If Not objFSO.FolderExists(newfolderpath) Then
 Set newfolder = objFSO.CreateFolder(newfolderpath)
End If
'----------------------------------------------------------------------------------------------------------------------------
'Now copy the encoded script file to users machine
strLocalfile = SysRoot & "\MyProgs\" 

If objFSO.FileExists(strLocalfile)=false Then
	objFSO.CopyFile  "\\server\share$\cleanmgr.vbs" , SysRoot & "\MyProgs\" , OverwriteExisting = true
end if
'----------------------------------------------------------------------------------------------------------------------------
'Time to create a new Scheduled Task
StartTime = "1300" 
StartString = StartTxt(StartTime)

'Configure the days to run the script. Only change Yes/No values.
Rundays=vbEmpty
Rundays = Rundays & RunTxt(Monday, "No", "1")
Rundays = Rundays & RunTxt(Tuesday, "No","2")
Rundays = Rundays & RunTxt(Wednesday, "No","4")
Rundays = Rundays & RunTxt(Thursday, "No","8")
Rundays = Rundays & RunTxt(Friday, "Yes","16")
Rundays = Rundays & RunTxt(Saturday, "No","32")
Rundays = Rundays & RunTxt(Sunday, "No","64")


If Left(Rundays,4) = " OR " Then
  Rundays = Right(Rundays,(Len(Rundays)-4))
End If

BuildTask StartString, eval(Rundays)

Sub BuildTask(StartString, Rundays)
      
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      
      Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
             errJobCreated = objNewJob.Create (SysRoot & "\MyProgs\Cleanmgr.vbs", StartString, False , Rundays, , ,"SystemCleanup")
End Sub

Function StartTxt(StartTime)
If StartTime = "1301" Then StartTime = "1300"
	'First get the systems time bias
	wmiQuery = "Select * from Win32_TimeZone"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(wmiQuery)
	For Each objItem in colItems
	  TimeBias = objItem.Bias
	Next
	If TimeBias>=0 Then
	  TimeBias="+" & right("000" & TimeBias,3)
	else
	  TimeBias="-" & right("000" & abs(TimeBias),3)
	end If
'Now build the time string
StartTxt = "********" & StartTime & "00.000000" & TimeBias
End Function


Function RunTxt(dayUnused,RunYesNo,DayVal)
  If Ucase(RunYesNo) = "YES" Then
             RunTxt = " OR " & DayVal & " "
  Else
      RunTxt = ""
  End If
End Function
'----------------------------------------------------------------------------------------------------------------------------
'Now that WMI created the task as AT1 lets Rename  the Scheduled Task to something more meaningful
strCurFol = SysRoot & "\Tasks"
objFSO.MoveFile strCurFol & "\At1.job" , strCurFol & "\Cleanmgr.job"
'----------------------------------------------------------------------------------------------------------------------------
'This needs to delete any At1 jobs created after the script has run
F = SysRoot & "\Tasks\At" 

For I = 1 To 20
  objFSO.DeleteFile F & I & ".job"
Next

Code:
Option Explicit
'On Error Resume Next
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Declare variables
Dim errJobCreated, JobID, fso, objShell, WshShell, Return
Dim objService, strComputer, objNewJob
Dim colScheduledJobs, objJob
Dim strStingerTaskExists, strCommand, strDaysOfWeek, strRunRepeatedly
Dim strStingerSource, strStingerDest, strStingerPath
Dim strCreateEventCMD1, strCreateEventCMD2
Dim strSysDrv, strSysRoot, strWinDir, strSchTasks
Dim objWMIService, colItems, objItem

strComputer = "."
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Check if this is a Server or Workstation and quit if you do not want it to run on those systems
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
    'If InStr(1,objItem.Caption,"Server") Then Wscript.Quit
	'If InStr(1,objItem.Caption,"Main System Chassis") Then Wscript.Quit
	'If InStr(1,objItem.Caption,"2000") Then Wscript.Quit
	If InStr(1,objItem.Caption,"98") Then Wscript.Quit
    'If InStr(1,objItem.Caption,"XP") Then Wscript.Quit
    'If InStr(1,objItem.Caption,"2003") Then Wscript.Quit
Next
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Add to event Viewer
	strStingerTaskExists = "N"
	strCreateEventCMD1 = "eventcreate /T Information /ID 999 /L Application /SO StingerJob-Good /D " & Chr(34) & "Stinger job created!" & Chr(34)
	strCreateEventCMD2 = "eventcreate /T Information /ID 999 /L Application /SO StingerJob-Bad /D " & Chr(34) & "Stinger job NOT created!" & Chr(34)
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Create enviroment variables
Set objService = GetObject("winmgmts:\\" & strComputer)
Set objNewJob = objService.Get("Win32_ScheduledJob")
Set fso = CreateObject("scripting.filesystemobject")
Set objShell = WScript.CreateObject("WScript.Shell")
Set WshShell = WScript.CreateObject("Wscript.Shell")

	strWinDir = objShell.ExpandEnvironmentStrings("%windir%")
	strSysDrv = objShell.ExpandEnvironmentStrings("%SystemDrive%")
	strSysRoot = objShell.ExpandEnvironmentStrings("%SystemRoot%")
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Copy a file to the system if needed
	strStingerSource = "\\server\share$\stinger.exe" 	'Where is the file
	strStingerDest = strSysDrv & "\"						'Where would you like the file copied to this is C:\
	strStingerPath = strSysDrv & "\"				'Where is the files new path

'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'If the scheduled job exists quit (no point in creating it again)
If fso.FileExists(strSysRoot & "\Tasks\Runstinger.job") Then
  'wscript.echo "Job Exists"
  wscript.quit
' Else
  'Wscript.Echo "An error occurred and will copy it over now"
'  fso.CopyFile strStingerSource,strStingerDest,true 
End If  
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Delete Stinger Job
'Dim Filesys, ScheduledFile
'	Set Filesys = CreateObject ("Scripting.FileSystemObject")
'	Set ScheduledFile = Filesys.GetFile(strSysRoot & "\tasks\RunStinger.job")
'		ScheduledFile.Delete
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Create the scheduled job
strSchTasks = strWinDir & "\system32\schtasks.exe /create /RU SYSTEM /SC Weekly /d ""TUE Thu""  /TN RunStinger /TR " 'Days of the week = " Mon Tue Wed Thu Fri" just add as needed
strSchTasks = strSchTasks & Chr(34) & strSysDrv & "\stinger.exe /go /log /silent /adl" & Chr(34)
strSchTasks = strSchTasks & " /ST 13:00:00"
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'If you need to check that the task was created
Return = WshShell.Run(strSchTasks,0,True)
'wscript.echo Return
If Return = 0 Then
   'Wscript.Echo "everything is good"
Else
   'Wscript.Echo "something is messed up"
End If
 
GrimR
Have a star as well.
Nice to have more than one solution to a problem.
We may be able to include this as part of our install package in the future. For our short term needs I think we will use the command line.

Thanks for the script!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top