2012-08-03 51 views
1

找到一個可視基本腳本來在任務調度程序中創建一個任務。即使它在運行,它也不會工作。我認爲這是因爲它使用了不同的用戶名。如果我手動在任務計劃程序中創建任務,它需要我的密碼並使用不同的用戶名。在此之上,我似乎無法找到一個方法來設置日期也,而不是唯一的一次:在Windows XP的VB腳本中的任務調度程序中創建一個任務

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

我想如果可能的話有最最短的和簡單的代碼。我不是VB的專家,請耐心等待。

+1

你需要指定一個命令來運行,「c:\ test.txt」不是一個命令。啓動cmd.exe並鍵入「at /?」獲得基本的幫助。 – 2012-08-03 20:01:25

回答

2

不幸的是,這些腳本沒有解決我自己的問題,因爲我需要在Windows XP中以當前用戶身份安裝任務。我編寫並測試了這些VBScript,試圖做到這一點,但他們確實增加了任務,並以其他方式工作。

此函數將任務添加到在系統帳戶下運行的任務計劃程序。我僅在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和一堆等地的一些文檔。

相關問題