更新:请注意,从 Powershell v3 开始(但仅在 Windows 2012 及更高版本下!)有新的 API,我发现它更具吸引力:
$taskPath = "\MyTasksFolder\"
$name = 'MyTask'
$runAt = '5:00 AM'
$exe = 'my.exe'
$params = 'command line arguments'
$location = "C:\Path\To\MyTask"
Unregister-ScheduledTask -TaskName $name -TaskPath $taskPath -Confirm:$false -ErrorAction:SilentlyContinue
$action = New-ScheduledTaskAction –Execute "$location\$exe" -Argument "$params" -WorkingDirectory $location
$trigger = New-ScheduledTaskTrigger -Daily -At $runAt
Register-ScheduledTask –TaskName $name -TaskPath $taskPath -Action $action –Trigger $trigger –User 'someuser' -Password 'somepassword' | Out-Null
Amal 的/v1
switch 解决方案很棒,但不允许在自定义文件夹中创建任务(即你不能创建“MyCompany\MyTask”,所有内容都在根文件夹中),所以我最终得到了下面描述的 PowerShell 脚本.
用法:
CreateScheduledTask -computer:"hostname-or-ip" `
-taskName:"MyFolder\MyTask" `
-command:"foo.exe" `
-arguments:"/some:args /here" `
-workingFolder:"C:\path\to\the\folder" `
-startTime:"21:00" `
-enable:"false" `
-runAs:"DOMAIN\user" `
-runAsPassword:"p@$$w0rd"
(注意,enable
必须是小写 - 对于您需要的布尔值 $value.ToString().ToLower()
)
执行:
该函数使用 XML 任务定义和“Schedule.Service”COM 对象。
#####################################################
#
# Creates a Windows scheduled task triggered DAILY.
# Assumes TODAY start date, puts "run-as" user as task author.
#
#####################################################
function CreateScheduledTask($computer, $taskName, $command, $arguments, $workingFolder, $startTime, $enable, $runAs, $runAsPassword)
{
$xmlTemplate = "<?xml version='1.0' encoding='UTF-16'?>
<Task version='1.2' xmlns='http://schemas.microsoft.com/windows/2004/02/mit/task'>
<RegistrationInfo>
<Date>{0}</Date>
<Author>{1}</Author>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>{2}</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id='Author'>
<UserId>{1}</UserId>
<LogonType>Password</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>{3}</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context='Author'>
<Exec>
<Command>{4}</Command>
<Arguments>{5}</Arguments>
<WorkingDirectory>{6}</WorkingDirectory>
</Exec>
</Actions>
</Task>"
$registrationDateTime = [DateTime]::Now.ToString("yyyy-MM-dd") + "T" + [DateTime]::Now.ToString("HH:mm:ss")
$startDateTime = [DateTime]::Now.ToString("yyyy-MM-dd") + "T" + $startTime + ":00"
$xml = $xmlTemplate -f $registrationDateTime, $runAs, $startDateTime, $enable, $command, $arguments, $workingFolder
$sch = new-object -ComObject("Schedule.Service")
$sch.Connect($computer)
$task = $sch.NewTask($null)
$task.XmlText = $xml
$createOrUpdateFlag = 6
$sch.GetFolder("\").RegisterTaskDefinition($taskName, $task, $createOrUpdateFlag, $runAs, $runAsPassword, $null, $null) | out-null
}