# Solved: VBScript - SCHTASKS.EXE Startup Script



## Achtontli (Nov 12, 2008)

Here is the code that I am attempting to use as a startup script deployed by GPO.
------
Set objWshNet = CreateObject("WScript.Network")
strComputer = objWshNet.ComputerName

Set fso = CreateObject("Scripting.FileSystemObject")
If Not FSO.FileExists("c:\Windows\tasks\Inventory.job") Then

sRndFreq = Random1DigitNumber(1,4)

If sRndFreq = 1 Then sFreq = "First"
If sRndFreq = 2 Then sFreq = "Second"
If sRndFreq = 3 Then sFreq = "Third"
If sRndFreq = 4 Then sFreq = "Last"


sRndDay = Random1DigitNumber(1, 5)

If sRndDay = 1 Then sDay = "MON"
If sRndDay = 2 Then sDay = "TUE"
If sRndDay = 3 Then sDay = "WED"
If sRndDay = 4 Then sDay = "THU"
If sRndDay = 5 Then sDay = "FRI"

sRndTime = Random2DigitNumber(08, 16) & ":" _
& Random2DigitNumber(0, 59) & ":" & Random2DigitNumber(0, 59)

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "schtasks.exe /create /s " & strComputer & " /tn Inventory /sc MONTHLY /mo " & sFreq & " /d " & sDay & " /st " _
& sRndTime & " /tr \\filesrv1\software\inventory\inventoryschedule.vbs /ru ""System""",0

Else
End If

Function Random1DigitNumber(iLower, iUpper)
Randomize
Random1DigitNumber = Right(Int((iUpper - iLower +1) * Rnd +iLower), 2)
End Function

Function Random2DigitNumber(iLower, iUpper)
Randomize
Random2DigitNumber = _
Right("00" & Int((iUpper - iLower + 1) * Rnd + iLower), 2)
End Function

-----

What the script does is creates a monthly scheduled task on a random day and time during the month. The purpose of that is to keep the task (which is actually another vbs file) from running on too many computers at once. It works if I run it locally, but it does not work during as a startup script. I have searched on the computer for the job and it is not on the target computer.

According the GPRESULT, the computer I am testing on is being affected by the GPO created for this purpose. I know the script itself running because I have added a msgbox to it to let me know that it is. I have tried multiple computers and it just won't take.

Can anybody see anything that would be causing the task to not be created through startup scripting?

Thanks!


----------



## Achtontli (Nov 12, 2008)

I think I found the solution:

Instead of:

WshShell.Run "schtasks.exe /create /s " & strComputer & " /tn Inventory /sc MONTHLY /mo " & sFreq & " /d " & sDay & " /st " _
& sRndTime & " /tr \\filesrv1\software\inventory\inventoryschedule.vbs /ru ""System""",*0*

I used:

WshShell.Run "schtasks.exe /create /s " & strComputer & " /tn Inventory /sc MONTHLY /mo " & sFreq & " /d " & sDay & " /st " _
& sRndTime & " /tr \\filesrv1\software\inventory\inventoryschedule.vbs /ru ""System""",*1, True*

Not sure what this does but it does properly populate the scheduled task.


----------

