创建任务计划
该脚本用来创建一次性和循环任务计划
1 #定义ERP升级检查任务计划的名称、启动时间、启动日期、运行任务计划的账户和密码、启动脚本路径 2 $TaskName = "2MyTask" 3 $Task_Starttime = "22:13" 4 $Task_StartDate = "2015/01/01" 5 $ScriptPath = "d:\abc.ps1" 6 $UserName = "administrator" 7 $UserPass = "pass" 8 #PowerShell脚本要通过调用PowerShell.exe执行 9 $TaskScriptPath = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe " + $ScriptPath 10 11 #定义函数TaskSch_Once,创建一次性任务计划 12 Function TaskSch_Once($TaskName,$UserName,$UserPass,$Task_Starttime,$Task_StartDate,$TaskScriptPath) 13 { 14 #如果该任务计划已存在,则先删除再新建,一次性任务计划不能够被直接修改 15 $Taskq = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskName -Encoding default -quiet 16 If ($Taskq -eq "true") 17 { 18 $Null = schtasks.exe /delete /tn $TaskName /f 19 $Null = schtasks.exe /create /tn $TaskName /sc once /ru $UserName /rp $UserPass /st $Task_Starttime /sd $Task_StartDate /tr $TaskScriptPath 20 } 21 Else {$Null = schtasks.exe /create /tn $TaskName /sc once /ru $UserName /rp $UserPass /st $Task_Starttime /sd $Task_StartDate /tr $TaskScriptPath} 22 $result = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskName -Encoding default 23 If ($result -ne $null) 24 {$result.Line} 25 Else { 26 Write-Host "$TaskName Task Created Failed" -ForegroundColor Red 27 } 28 } 29 30 #定义函数TaskSch_Cycle,定义循环任务计划 31 Function TaskSch_Cycle($TaskName,$UserName,$UserPass,$Task_Starttime,$Task_StartDate,$TaskScriptPath) 32 { 33 #如果该任务计划已存在,则直接进行直接修改,否则将创建新的任务计划 34 $Taskq = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskName -Encoding default -quiet 35 If ($Taskq -eq "true") 36 {$Null = schtasks.exe /change /tn $TaskName /ru $UserName /rp $UserPass /Enable} 37 Else {$Null = schtasks.exe /create /tn $TaskName /sc minute /mo 1 /ru $UserName /rp $UserPass /st $Task_Starttime /sd $Task_StartDate /tr $TaskScriptPath} 38 $result = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskName -Encoding default 39 If ($result -ne $null) 40 {$result.Line} 41 Else { 42 Write-Host "$TaskName Task Created Failed" -ForegroundColor Red 43 } 44 } 45 46 #创建一次性任务计划 47 #TaskSch_Once $TaskName $UserName $UserPass $Task_Starttime $Task_StartDate $TaskScriptPath 48 49 #创建循环任务计划 50 TaskSch_Cycle $TaskName $UserName $UserPass $Task_Starttime $Task_StartDate $TaskScriptPath
#创建可见任务计划,每月15号4:00运行一次
$TaskName = "log_bak" $UserName = "administrator" $UserPass = "password" $Task_Starttime = "04:00" $Task_StartDate = "2015/09/01" $TaskScriptPath = "Powershell.exe E:\Operations\Scripts\logbak.ps1" schtasks.exe /create /tn $TaskName /sc monthly /d 15 /ru $UserName /rp $UserPass /st $Task_Starttime /sd $Task_StartDate /tr $TaskScriptPath /v1
#创建任务计划,每分钟运行一次
$TaskWatchName = "rsync-task" $UserName = "administrator" $UserPass = "n" $TaskWatchtime = "00:01" $TaskWatchDate = "2015/12/01" $TaskWatchScriptPath = "powershell.exe D:\\Scripts\rsync-task.ps1" schtasks /create /tn $TaskWatchName /sc minute /mo 1 /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath
#创建可见任务计划,在系统启动时自动运行
$TaskName = "taskname" $UserName = "administrator" $UserPass = "password" $TaskScriptPath = "D:\startup.bat" schtasks.exe /create /tn $TaskName /sc onlogon /ru $UserName /rp $UserPass /tr $TaskScriptPath /v1
#创建任务计划,用户登录后延迟30S运行 $Dest_path = "d:\" $TaskName = "AddUser" $UserName = "uxin\user" $UserPass = 'password' $ScriptName = "AddUser.ps1" $Script_path = Join-Path $Dest_path $ScriptName $TaskScriptPath = "PowerShell.exe $Script_path" schtasks.exe /create /tn $TaskName /ru $UserName /rp $UserPass /sc onlogon /tr $TaskScriptPath /delay 0000:30 /f #创建任务计划,以Administrators组用户运行 $TaskName = "LogonPrompt" $ScriptName = "LogonPrompt.vbs" $Script_path = Join-Path $Dest_path $ScriptName $TaskScriptPath = $Script_path schtasks.exe /create /tn $TaskName /s $env:computername /ru administrators /sc onlogon /tr $TaskScriptPath /f
参考:
- 常规月计划语法
- schtasks /create /tn TaskName /tr TaskRun /sc monthly [/mo {FIRST | SECOND | THIRD | FOURTH | LAST | LASTDAY] [/d {MON - SUN | 1 - 31}] [/m {JAN - DEC[,JAN - DEC...] | *}] [/st StartTime] [/sd StartDate] [/ed EndDate] [/s computer [/u [domain\]user /ppassword]] [/ru {[Domain\]User | "System"} [/rp Password]]
- 指定周的语法
- schtasks /create /tn TaskName /tr TaskRun /sc monthly /mo {FIRST | SECOND | THIRD | FOURTH | LAST} /d {MON - SUN} [/m {JAN - DEC[,JAN - DEC...] | *}] [/st StartTime] [/sd StartDate] [/ed EndDate] [/s computer [/u [domain\]user /p password]] [/ru{[Domain\]User | "System"} [/rp Password]]
- Lastday 语法
- schtasks /create /tn TaskName /tr TaskRun /sc monthly /mo LASTDAY /m {JAN - DEC[,JAN - DEC...] | *} [/st StartTime] [/sd StartDate] [/ed EndDate] [/s computer [/u [domain\]user /p password]] [/ru {[Domain\]User | "System"} [/rp Password]]
- 指定日期的语法
- schtasks /create /tn TaskName /tr TaskRun /sc monthly /d {1 - 31} [/m {JAN - DEC[,JAN - DEC...] | *}] [/st StartTime] [/sd StartDate] [/ed EndDate] [/s computer [/u [domain\]user /p password]] [/ru {[Domain\]User | "System"} [/rp Password]]
from:http://www.cnblogs.com/lostyue/archive/2011/10/24/2223166.html