2016-06-23 31 views
0

我正在使用底部的代码创建一个计划任务,它在设置完成后会在30秒内运行。它以记事本为例。原始程序需要管理员权限才能运行。所以需要设置以最高权限运行设置。如何使用VBScript进行设置?重要的一点是,VBScript不能要求管理员权限才能运行此代码来设置此选项。使用VBScript创建时间表任务作为管理员运行而不要求管理员权限?

' This sample schedules a task to start notepad.exe 30 seconds 
' from the time the task is registered. 
'------------------------------------------------------------------ 

' A constant that specifies a time-based trigger. 
const TriggerTypeTime = 1 
' A constant that specifies an executable action. 
const ActionTypeExec = 0 

'******************************************************** 
' Create the TaskService object. 
Set service = CreateObject("Schedule.Service") 
call service.Connect() 

'******************************************************** 
' Get a folder to create a task definition in. 
Dim rootFolder 
Set rootFolder = service.GetFolder("\") 

' The taskDefinition variable is the TaskDefinition object. 
Dim taskDefinition 
' The flags parameter is 0 because it is not supported. 
Set taskDefinition = service.NewTask(0) 

'******************************************************** 
' Define information about the task. 

' Set the registration info for the task by 
' creating the RegistrationInfo object. 
Dim regInfo 
Set regInfo = taskDefinition.RegistrationInfo 
regInfo.Description = "Start notepad at a certain time" 
regInfo.Author = "Administrator" 

' Set the task setting info for the Task Scheduler by 
' creating a TaskSettings object. 
Dim settings 
Set settings = taskDefinition.Settings 
settings.Enabled = True 
settings.StartWhenAvailable = True 
settings.Hidden = False 

'******************************************************** 
' Create a time-based trigger. 
Dim triggers 
Set triggers = taskDefinition.Triggers 

Dim trigger 
Set trigger = triggers.Create(TriggerTypeTime) 

' Trigger variables that define when the trigger is active. 
Dim startTime, endTime 

Dim time 
time = DateAdd("s", 30, Now) 'start time = 30 seconds from now 
startTime = XmlTime(time) 

time = DateAdd("n", 5, Now) 'end time = 5 minutes from now 
endTime = XmlTime(time) 

WScript.Echo "startTime :" & startTime 
WScript.Echo "endTime :" & endTime 

trigger.StartBoundary = startTime 
trigger.EndBoundary = endTime 
trigger.ExecutionTimeLimit = "PT5M" 'Five minutes 
trigger.Id = "TimeTriggerId" 
trigger.Enabled = True 

'*********************************************************** 
' Create the action for the task to execute. 

' Add an action to the task to run notepad.exe. 
Dim Action 
Set Action = taskDefinition.Actions.Create(ActionTypeExec) 
Action.Path = "C:\Windows\System32\notepad.exe" 

WScript.Echo "Task definition created. About to submit the task..." 

'*********************************************************** 
' Register (create) the task. 

call rootFolder.RegisterTaskDefinition(_ 
    "Test TimeTrigger", taskDefinition, 6, , , 3) 

WScript.Echo "Task submitted." 

'------------------------------------------------------------------ 
' Used to get the time for the trigger 
' startBoundary and endBoundary. 
' Return the time in the correct format: 
' YYYY-MM-DDTHH:MM:SS. 
'------------------------------------------------------------------ 
Function XmlTime(t) 
    Dim cSecond, cMinute, CHour, cDay, cMonth, cYear 
    Dim tTime, tDate 

    cSecond = "0" & Second(t) 
    cMinute = "0" & Minute(t) 
    cHour = "0" & Hour(t) 
    cDay = "0" & Day(t) 
    cMonth = "0" & Month(t) 
    cYear = Year(t) 

    tTime = Right(cHour, 2) & ":" & Right(cMinute, 2) & _ 
     ":" & Right(cSecond, 2) 
    tDate = cYear & "-" & Right(cMonth, 2) & "-" & Right(cDay, 2) 
    XmlTime = tDate & "T" & tTime 
End Function 
+1

的“如果这是可能的”思想实验将帮助你理解为什么这是不可能的。 (所有恶意软件都可以做到!) –

回答

2

您需要管理员权限才能配置任务以使用管理员权限运行。所以,基本上你所要求的是不可能的。

1

Actualy,这是在旧版本的Windows的问题,并通过恶意软件被广泛应用于