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")
For Each objJob in colScheduledJobs
If objJob.Command = TaskToTest Then
WScript.Echo "Task already created, exiting."
WScript.Quit
End If
Next
If DSTEffective = True Then 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)
Select Case mode
Case "AM" strheure=Left (heure,Len(heure)-3)
test=Len(strheure)
If test =5 Then
strheure = "0" & strheure
End if
MyTime= strheure
Case "PM" strheure=Left (heure,Len(heure)-3)
test=Len(strheure)
If test =5 Then
strheure = "0" & strheure
End if
MyTime= strheure + 120000
Case Else MyTime=heure
End Select
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 = 0 Then
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
End If
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 )
For Each 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
End With
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
End If
End Function
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 = 1 To 31
If myWoM > j Then
If IsDate( myYear & "-" & myMonth & "-" & i ) Then
If DatePart( "w", CDate( myYear & "-" & myMonth & "-" & i ), vbSunday ) = myDoW + 1 Then
j = j + 1
LastDoW = i
End If
End If
End If
Next
End Function
Function TimeZoneOffset
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objTimeZone in colTimeZone
strTimeZoneOffset = objTimeZone.Bias
Next
End Function