Plan for failures, hope for success!
Updating Scheduled Tasks Via VB Script
So I found myself in a situation where I needed to update over 100 scheduled tasks and I really did not feel like doing them by hand, so I thought I’d create a script that would update them for me.
Here is what i came up with:
Thats pretty much it, Out of about 200 scheduled tasks this script found and updated about 105 of them that needed to be changed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<span style="color: #339966;">'create your shell object </span>Set objShell = WScript.CreateObject("WScript.Shell") <span style="color: #339966;">'create the environment varaible ojbect </span>Set varEnv = objShell.Environment("Process") <span style="color: #339966;">'set Temp to varTemp </span>varTemp = varEnv("TEMP") <span style="color: #339966;">'now lets read in all the scheduled tasks in the system </span>On Error Resume Next cmdResult = objShell.Run("%comspec% /c schtasks /query /v /nh /fo CSV >" & varTemp & "\schtasks.txt", 1, true) Select Case Err.Number Case 0 <span style="color: #339966;">'no error </span>Case Else msgbox "Error Reading Scheduled Tasks" Wscript.Quit End Select On Error Goto 0 <span style="color: #339966;">'Ok so we have the file created with the scheduled tasks, lets read it into the script 'Create the file system Object</span> set objFSO = CreateObject("Scripting.FileSystemObject") <span style="color: #339966;">'Open the File with the schedule task info we created earlier </span>set schTasks = objFSO.OpenTextFile(varTemp & "\schtasks.txt", 1, false) <span style="color: #339966;">'read the file into memory </span>schContent = schTasks.ReadAll <span style="color: #339966;">'close the file </span>schTasks.close arrSchTasks = Split(schContent, vbCrLf) For Each strTask in arrSchTasks <span style="color: #339966;">'If the word "\batch\\" is found, then process the string</span> If (InStr(1,strTask,"\batch\",1) <> 0) Then <span style="color: #339966;">'schTasks /Query will return commas in the date field, this is a good way to split the CSV (",") </span> arrTask = Split(strTask,Chr(34)&","&Chr(34)) <span style="color: #339966;">'this is an extra check to make sure the array is not empty for some reason (if it was, it would return -1) </span> If ubound(arrTask) > 0 then varTaskRun = Replace(arrTask(9),"\batch\","scripts\",1,-1,1) On Error Resume Next <span style="color: #339966;">'Now that we edited the text we needed, lets write it back to the Task List</span> <span style="color: #339966;"> 'Make sure you set RU and RP with YOUR user name and Password for the account that runs your scheduled tasks </span> cmdResult = objShell.Run("%comspec% /c schtasks /change /RU Pinchii\taskman /RP Password123 /TN " &Chr(34)&arrTask(1) &Chr(34)& " /TR " & Chr(34) & varTaskRun & Chr(34), 1, true) Select Case Err.Number Case 0 <span style="color: #339966;">'no error </span> Wscript.Write "Updated " & arrTask(1) Case Else msgbox "Error Setting " & arrTask(1) End Select On Error Goto 0 End If End If Next |
Comments are closed.