locked
VBS that runs fine under Windows 7 won't create scheduled task under Windows 10 even if it say's the task was created RRS feed

  • Question

  • The following script create a scheduled task under Windows 7 but not under Windows 10, even basic script samples to create a scheduled task that used to work on Windows 7 don't work on Windows 10. Are there any major changes to VBS from Windows 7 to Windows 10 that could prevent this script from running fine?

    '==========================================================================
    '
    ' VBScript Source File 
    '
    ' NAME: 
    '
    ' AUTHOR: Marc Sanschagin
    ' DATE  : 2012-03-06
    '
    ' COMMENT: 
    '
    '==========================================================================
    'Command,StartTime,RunRepeatedly,DaysOfWeek,DaysOfMonth,InteractWithDesktop,JobID
    '***Description of properties***
    '
    '*DaysOfMonth* Days of the month when the job is scheduled to run.
    'If a job is to run on multiple days of the month, these values can be joined in a logical OR.
    'For example, if a job is to run on the 1st and 16th of each month, the value of the DaysOfMonth property will be 1 OR 32768.
    'Possible values are:
    '1 - 1st
    '2 - 2nd
    '4 - 3rd
    '8 - 4th
    '16 - 5th
    '32 - 6th
    '64 - 7th
    '128 - 8th
    '256 - 9th
    '512 - 10th
    '1024 - 11th
    '2048 - 12th
    '4096 - 13th
    '8192 - 14th
    '16384 - 15th
    '32768 - 16th
    '65536 - 17th
    '131072 - 18th
    '262144 - 19th
    '524288 - 20th
    '1048576 - 21st
    '2097152 - 22nd
    '4194304 - 23rd
    '8388608 - 24th
    '16777216 - 25th
    '33554432 - 26th
    '67108864 - 27th
    '134217728 - 28th
    '268435456 - 29th
    '536870912 - 30th
    '1073741824 - 31st

    '*DaysOfWeek*
    'Days of the week when the job is scheduled to run.
    'If a job is to run on multiple days of the week, these values can be joined in a logical OR.
    'For example, if a job is to run on Mondays, Wednesdays, and Fridays, the value of the DaysOfWeek property will be 1 OR 4 OR 16.
    'Possible values are:
    '1 - Monday
    '2 - Tuesday
    '4 - Wednesday
    '8 - Thursday
    '16 - Friday
    '32 - Saturday
    '64 - Sunday


    strComputer = "."

    TimeDelay = "15"'Time Delay after wich the scheduled task will execute
    RUN_REPEATEDLY =False'If the task need to be run only once set to False if it as to run on a regular basis set to True
    'DaysOfMonth =  'Uncomment and Define if required otherwise just use a coma
    'DaysofWeek=    'Uncomment and Define if required otherwise just use a coma
    InteractWithUser =False
    TaskToRun=chr(34) & "C:\windows\system32\msiexec.exe" & chr(34) & " " & "/I C:\windows\support\sources\DotSoft_ToolPAC_14\ToolPac14.msi LAUNCHREADME=-2 ALLUSERS=1 USERPROFILE=C:\Users\public\ SHOWLAUNCHREADME=0 /l*v c:\windows\support\sources\DotSoft_ToolPAC_14\msi_install.log /qb"
    TaskToTest= "C:\windows\system32\msiexec.exe /I C:\windows\support\sources\DotSoft_ToolPAC_14\ToolPac14.msi LAUNCHREADME=-2 ALLUSERS=1 USERPROFILE=C:\Users\public\ SHOWLAUNCHREADME=0 /l*v c:\windows\support\sources\DotSoft_ToolPAC_14\msi_install.log /qb"
    Modifier= "00"


    Dim MyTime
    Dim strTimeZoneOffset


    dtmDate = Now

    blnDST = isDST( dtmDate )
    DSTEffective =  blnDST


    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colScheduledJobs = objWMIService.ExecQuery  ("SELECT * FROM Win32_ScheduledJob")
    ForEach objJob in colScheduledJobs

    If objJob.Command = TaskToTest Then
     WScript.Echo "Task already created, exiting."
        WScript.Quit
    EndIf
    Next


    If DSTEffective = TrueThen Modifier = "-01"

    heure = Time + TimeSerial (00,TimeDelay,00) 'Set the time delay to add before execution hours, minutes, seconds
    heure = heure + TimeSerial (Modifier,00,00) 'Adjustment to Daylight saving Time
    heure = Replace (heure, ":","")

    mode=Right(heure,2)


    SelectCase mode
      Case"AM" strheure=Left (heure,Len(heure)-3)
                test=Len(strheure)
                If test =5Then
                strheure = "0" & strheure
                Endif
                MyTime= strheure
                            
      Case"PM" strheure=Left (heure,Len(heure)-3)
                test=Len(strheure)
                If test =5Then
                strheure = "0" & strheure
                Endif
                MyTime= strheure + 120000
                
            
      CaseElse MyTime=heure
      EndSelect
      
      
      
      TimeZoneOffset
      
      
      strWhen = "********" & MyTime & ".000000" & strTimeZoneOffset

      
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
    errJobCreated = objNewJob.Create (TaskToRun, strWhen,RUN_REPEATEDLY , , ,InteractWithUser , JobID) 

    If errJobCreate = 0Then
        WScript.Echo "Job created successfully: " & VBcrlf & "DotSoft ToolPack 14 setup scheduled to run in " & TimeDelay & " minutes"
    Else
        WScript.Echo "Job not created. Error code = " & errJobCreated
    EndIf

    WScript.quit


    Function isDST( myDate )
    ' Returns TRUE if the specified date is in daylight Saving Time, or FALSE if not.
    ' The function ignores the 'ambiguous hour' right after the Standard Time transition.
    '   Dim myDay, myHour, myMinute, myMonth, myYear
    '   Dim lngDaylight, lngLocDate, lngStandard
    '   Dim colItems, objItem, objWMIService
    '   Dim strTZDST, strTZStd

        ' Parse the specified date/time
        myDate   = CDate( myDate )
        myDay    = DatePart( "d",    myDate )
        myMonth  = DatePart( "m",    myDate )
        myYear   = DatePart( "yyyy", myDate )
        myHour   = DatePart( "h",    myDate )
        myMinute = DatePart( "n",    myDate )

        Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
        Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_TimeZone", , 48 )

        ForEach objItem in colItems
            With objItem
                ' Convert the Daylight Start date/time, specified date/time and Standard Start date/time to long integers (YYYYMMDDHHmm)
                lngDaylight = .DaylightMinute + 100 * .DaylightHour + 10000 * LastDoW( .DaylightDayOfWeek, .DaylightMonth, myYear, .DaylightDay ) + 1000000 * .DaylightMonth + 100000000 * myYear
                lngLocDate  =  myMinute       + 100 *  myHour       + 10000 * myDay                                                               + 1000000 *  myMonth       + 100000000 * myYear
                lngStandard = .StandardMinute + 100 * .StandardHour + 10000 * LastDoW( .StandardDayOfWeek, .StandardMonth, myYear, .StandardDay ) + 1000000 * .StandardMonth + 100000000 * myYear
                ' Store the names for DST and Standard Time
                strTZDST    = .DaylightName
                strTZStd    = .StandardName
            EndWith
        Next

        Set colItems = Nothing
        Set objWMIService = Nothing

        ' Now that we have the long integers for the date/times, the actual comparison is quite straightforward
        If lngLocDate < lngDaylight Then
            isDST       = False
            strTimeZone = strTZStd
        ElseIf lngLocDate >= lngStandard Then
            isDST       = False
            strTimeZone = strTZStd
        Else
            isDST       = True
            strTimeZone = strTZDST
        EndIf
    EndFunction

    Function LastDoW( myDoW, myMonth, myYear, myWoM )
    ' Returns the day of the month for the specified week day, month, year and week of month
    ' e.g. LastDoW( 0, 3, 2011, 5 ) will return the last (5) Sunday (0) of March (3) 2011, which is 27
        Dim i, j
        j = 0
        LastDoW = 0
        For i = 1To31
            If myWoM > j Then
                IfIsDate( myYear & "-" & myMonth & "-" & i ) Then
                    IfDatePart( "w", CDate( myYear & "-" & myMonth & "-" & i ), vbSunday ) = myDoW + 1Then
                            j = j + 1
                            LastDoW = i
                    EndIf
                EndIf
            EndIf
        Next
    EndFunction

    Function TimeZoneOffset
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")

    ForEach objTimeZone in colTimeZone
        strTimeZoneOffset = objTimeZone.Bias 
    Next
    EndFunction

    Friday, August 12, 2016 1:15 PM

Answers

  • You mentioned that the Win32_ScheduledJob was deprecated for some time, can you tell me where to find references about deprecated functions or objects in VBS?

    Regarding Win32_ScheduledJob, it's right there in the documentation for that WMI class.

    The Win32_ScheduledJob class is internally using the AT protocol, which is bound to deprecation starting with Windows 8 and Windows Server 2012.

    Is there a list of other objects that are deprecated? I don't know. It depends on the object.


    -- Bill Stewart [Bill_Stewart]

    Friday, August 12, 2016 4:24 PM

All replies

  • Win32_ScheduledJob has been deprecated for some time.

    Also, start by reading the very first post from right at the top of this forum:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Friday, August 12, 2016 1:57 PM
  • Bill,

    The question is still relevant, but may have been formulated as:

    Are there any major changes to VBS from Windows 7 to Windows 10 that could prevent a VB script from creating a scheduled task?

    Instead of:

    Are there any major changes to VBS from Windows 7 to Windows 10 that could prevent this script from running fine?

    The script is there just a reference.

    Marc

    Friday, August 12, 2016 3:17 PM
  • Will the answers make any difference? If it doesn't work, it doesn't work.

    You'll need to use the schtasks.exe command or the Schedule.Service COM object instead.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Friday, August 12, 2016 4:26 PM
    Friday, August 12, 2016 3:42 PM
  • The path for your executable does not exist in Windows 10.


    \_(ツ)_/

    Friday, August 12, 2016 3:44 PM
  • Bill,

    With your tip on the Schedule.Service COM object, I have found this article:

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa383665(v=vs.85).aspx

    With this information I can rework my script, so that it work under Windows 10.

    You mentioned that the Win32_ScheduledJob was deprecated for some time, can you tell me where to find references about deprecated functions or objects in VBS?

    It could be usefull for other scripter's too.

    Thanks

    Marc

    Friday, August 12, 2016 4:05 PM
  • You mentioned that the Win32_ScheduledJob was deprecated for some time, can you tell me where to find references about deprecated functions or objects in VBS?

    Regarding Win32_ScheduledJob, it's right there in the documentation for that WMI class.

    The Win32_ScheduledJob class is internally using the AT protocol, which is bound to deprecation starting with Windows 8 and Windows Server 2012.

    Is there a list of other objects that are deprecated? I don't know. It depends on the object.


    -- Bill Stewart [Bill_Stewart]

    Friday, August 12, 2016 4:24 PM