Advertisement
NiXC

Create an immediate scheduled task, vbscript

Sep 4th, 2011
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. Function CreateJob(strCommand)
  2. Const SHELL_WAIT = True
  3. Const SHELL_HIDE = 0
  4. CreateJob = False
  5. ' Get date & time 1 minute in advance
  6. ' And it must be at least 1 minute
  7. ' Source: w3schools, & mikeblas on hardforum.com
  8. Dim strDateTime : strDateTime = DateAdd("n", 1, Now())
  9. Dim strDate : strDate = LEFT(strDateTime, InStr(strDateTime, " ")-1)
  10. Dim strTime : strTime = MID(strDateTime, InStr(strDateTime, " ")+1)
  11.  
  12. ' define the command we will run to create the once-only scheduled task
  13. ' uses a new guid for the name each time so it will be a unique task
  14. Dim strJobCmd : strJobCmd = "schtasks.exe /Create /TN " & _
  15. getGuid & " /RU SYSTEM /ST " & _
  16. strTime & " /SD " & _
  17. strDate & " /SC ONCE /TR """ & _
  18. strCommand & """"
  19. ' on Vista/Win7 must create task as XP-readable type using /V1
  20. ' this is so it will delete itself propely (bug in schtasks) using /Z
  21. If onVistaWin7 Then strJobCmd = strJobCmd & " /Z /V1"
  22. WScript.echo strJobCmd
  23. Dim oJobShell : Set oJobShell = CreateObject("WScript.Shell")
  24. Dim jobRet : jobRet = oJobShell.Run(strJobCmd, SHELL_HIDE, SHELL_WAIT)
  25. If jobRet = 0 Then CreateJob = True
  26. ' here we tried to make the task and get the result to a variable
  27. ' if the return is non-zero then the creation of the task errored
  28. Set oJobShell = Nothing
  29. End Function
  30.  
  31. Function getGuid
  32. ' this functions gets a unique guid and returns it as a string
  33. Dim TypeLib : Set TypeLib = CreateObject("Scriptlet.TypeLib")
  34. getGuid = Left(CStr(TypeLib.Guid),38)
  35. ' above line also removes some strageness at the end
  36. Set TypeLib = Nothing
  37. End Function
  38.  
  39. Function onVistaWin7
  40. ' this function returns true on Vista or above (incl. Srv2008)
  41. Dim colOSver, objOSver
  42. onVistaWin7 = False
  43. Set colOSver = GetObject("WinMgmts:root\cimv2").ExecQuery("Select Version from Win32_OperatingSystem")
  44. For Each objOSver In colOSver
  45. If Left(objOSver.Version,1) >= 6 Then onVistaWin7 = True
  46. Next
  47. Set colOSver = Nothing
  48. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement