1

タスクスケジューラでタスクを作成するためのビジュアルベーシックスクリプトを見つけました。それでも、実行中と表示されていても機能しません。これは、別のユーザー名を使用しているためだと思います。タスクスケジューラでタスクを手動で作成する場合、パスワードが必要であり、別のユーザー名を使用します。これに加えて、時間だけでなく日付も設定する方法を見つけることができないようです。

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

可能であれば、最も短くて単純なコードが欲しいです。私はVBの専門家ではないので、ご容赦ください。

4

1 に答える 1

2

残念ながら、Windows XPに現在のユーザーとしてタスクをインストールする必要があるため、これらのスクリプトはいずれも自分の問題を解決しませんでした。私はこれらのVBScriptを作成してテストし、それを実行しようとしましたが、タスクを追加したり、その他の方法で機能したりします。

この関数は、システムアカウントで実行されるタスクスケジューラにタスクを追加します。これはWindowsXPでのみテストしましたが、他のバージョンのWindowsでも機能すると思います。ニーズに合うように少し調整する必要があるかもしれません。これに基づい

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

この機能はWindows7では機能しますが、WindowsXPでは機能しません。他のWindowsバージョンはテストしていません。これに基づい

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を気にしない場合は、次のシェルコマンドで実行できます。

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

/fWindows XPでは無効ですので、使用しないでください。残念ながら、Windows XPでは、これにより現在のユーザーのパスワードの入力を求められます。

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

このコマンドに関するいくつかのドキュメントがここここにあり、他にもたくさんあります。

于 2013-04-26T13:16:09.560 に答える