在Windows脚本的VB脚本中的任务调度程序中创建任务

时间:2012-08-03 19:56:27

标签: vbscript

找到一个可视化基本脚本,用于在任务计划程序中创建任务。然而即使它说它正在运行它也不会起作用。我认为这是因为它使用了不同的用户名。如果我手动在任务计划程序中创建任务,则需要我的密码并使用不同的用户名。除此之外,我似乎无法找到设置日期的方法,而不仅仅是时间:

Set ObjShell = CreateObject("WScript.Shell")
objShell.run "AT 10:00 C:\Test.txt"
Set ObjShell = nothing

如果可能的话,我想拥有最简短的代码。我不是VB的专家,所以请耐心等待。

1 个答案:

答案 0 :(得分:2)

不幸的是,对于我来说,这些脚本都没有解决我自己的问题,因为我需要在Windows XP中安装任务作为当前用户。我编写并测试了这些VBScripts以尝试做到这一点,但他们确实添加了任务并以其他方式工作。

此功能将任务添加到在系统帐户下运行的任务计划程序。我只在Windows XP中对此进行了测试,但我猜它仍适用于其他版本的Windows。您可能需要稍微调整一下以使其符合您的需求。基于this

Function ScheduleTaskWinXP(taskName)

    Dim strComputer
    strComputer = "."

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

    ' Win32_ScheduledJob class
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa394399(v=vs.85).aspx

    Dim objNewJob
    Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

    ' Create method of the Win32_ScheduledJob class
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa389389(v=vs.85).aspx

    Dim errJobCreated
    errJobCreated = objNewJob.Create("Notepad.exe", _
        "********123000.000000-420", True , _
        1 OR 4 OR 16, , , taskName) 

    Out errJobCreated
End Function

此功能适用于Windows 7,但在Windows XP中不起作用。我没有测试其他Windows版本。基于this

Function ScheduleTaskWin7(taskName)

    ' Task Scheduler Scripting Objects
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa383607(v=vs.85).aspx

    '------------------------------------------------------------------
    ' This sample schedules a task to start on a weekly basis.
    '------------------------------------------------------------------

    ' A constant that specifies a weekly trigger.
    const TriggerTypeWeekly = 3
    ' A constant that specifies an executable action.
    const ActionTypeExec = 0   

    '********************************************************
    ' Create the TaskService object.
    Dim service
    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.

    ' RegistrationInfo object
    ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa382100(v=vs.85).aspx

    ' Set the registration info for the task by 
    ' creating the RegistrationInfo object.
    Dim regInfo
    Set regInfo = taskDefinition.RegistrationInfo
    regInfo.Description = "Start Notepad weekly."
    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 weekly trigger. Note that the start boundary 
    ' specifies the time of day that the task starts, the 
    ' day-of-week specfies on what day of the week the task 
    ' runs, and the interval specifies what weeks the task runs.
    Dim triggers
    Set triggers = taskDefinition.Triggers

    Dim trigger
    Set trigger = triggers.Create(TriggerTypeWeekly)

    ' Trigger variables that define when the trigger is active 
    ' and the time of day that the task is run. The format of 
    ' this tims is YYYY-MM-DDTHH:MM:SS
    Dim startTime, endTime

    Dim time
    startTime = "2006-05-02T08:00:00"  'Task runs at 8:00 AM
    endTime = "2015-05-02T08:00:00"

    Out "startTime :" & startTime
    Out "endTime :" & endTime

    trigger.StartBoundary = startTime
    trigger.EndBoundary = endTime
    trigger.DaysOfWeek = 1
    trigger.WeeksInterval = 1    'Task runs every week.
    trigger.Id = "WeeklyTriggerId"
    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"

    Out "Task definition created. About to submit the task..."

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

    call rootFolder.RegisterTaskDefinition(taskName, taskDefinition, 6, , , 3)

    Out "Task submitted."

End Function

如果要在当前用户下创建任务而不关心Windows XP,则此shell命令将执行此操作:

schtasks /create /tn "TaskName" /tr "Executable.exe" /sc HOURLY /f

/f在Windows XP中无效,因此请勿使用它。不幸的是,在Windows XP中,这将提示输入当前用户的密码。

schtasks /create /tn "TaskName" /tr "Executable.exe" /sc HOURLY

此命令herehere以及其他一些地方都有一些文档。