Windows 使用PowerShell创建计划任务

Windows 使用PowerShell创建计划任务,windows,powershell,scheduled-tasks,powershell-4.0,azure-powershell,Windows,Powershell,Scheduled Tasks,Powershell 4.0,Azure Powershell,我正在编写脚本,该脚本将在第二天执行,然后每隔1、2和3天运行一次。这是我的剧本: #The script below will run as the specified user (you will be prompted for credentials) #PATH C:\backup\scripts\ will need to be replaced and created in preferred location where the respective backup scripts

我正在编写脚本,该脚本将在第二天执行,然后每隔1、2和3天运行一次。这是我的剧本:

#The script below will run as the specified user (you will be prompted for credentials)
#PATH C:\backup\scripts\ will need to be replaced and created in preferred location where the respective backup scripts will be stored

$jobname0 = "Full Backup"
$jobname1 = "Incremental Backup1"
$jobname2 = "Incremental Backup2"
# Change these script location to whatever you want
$script0 = "C:\backup\scripts\full.bat"
$script1 =  "C:\backup\scripts\inc1.cmd"
$script2 =  "C:\backup\scripts\inc2.ps1"
$repeat = (New-TimeSpan -Hours 72 )


# and is set to be elevated to use the highest privileges.
# The task will run every 72hrs (3days) specified in $repeat.
$action0 = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script0; quit"
$action1 = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script1; quit"
$action2 = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script2; quit"

$duration = (New-TimeSpan -Days (365 * 20))
$trigger0 = New-ScheduledTaskTrigger -Once -At 00:00AM -RepetitionInterval $repeat -RepetitionDuration $duration
$trigger1 = New-ScheduledTaskTrigger -Once -At 00:01AM -RepetitionInterval $repeat -RepetitionDuration $duration
$trigger2 = New-ScheduledTaskTrigger -Once -At (00:01AM).AddTime(48:00) -RepetitionInterval $repeat -RepetitionDuration $duration

$msg = "Enter the username and password that will run the task"; 
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd

Register-ScheduledTask -TaskName $jobname0 -Action $action0 -Trigger $trigger0 -RunLevel Highest -User $username -Password $password -Settings $settings 
Register-ScheduledTask -TaskName $jobname1 -Action $action1 -Trigger $trigger1 -RunLevel Highest -User $username -Password $password -Settings $settings 
Register-ScheduledTask -TaskName $jobname2 -Action $action2 -Trigger $trigger2 -RunLevel Highest -User $username -Password $password -Settings $settings
如何在触发器中添加天数而不是时间。非常感谢您的帮助。

准确回答您的问题: 只需将
$repeat=(New TimeSpan-Hours 72)
替换为
$repeat=(New TimeSpan-Days 3)
,就可以添加天数而不是时间小时


要添加一些其他信息和提示: 考虑使用而不是这一大堆变量。使用变量的优点是可重用性。当您定义(例如,
$jobname0
仅将其用作参数时,它(几乎)没有优势

您的代码可能如下所示

$credential = $Host.UI.PromptForCredential(
    "Task username and password",
    "Enter the username and password that will run the task",
    "$env:userdomain\$env:username",
    $env:userdomain
)

$repeat = (New-TimeSpan -Hours 72 )
$duration = (New-TimeSpan -Days (365 * 20))
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd

$job0 = @{
    "TaskName" = "Full Backup";
    "Action"   = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument "C:\backup\scripts\full.bat;quit";
    "Trigger"  = New-ScheduledTaskTrigger -Once -At 00:00AM -RepetitionInterval $repeat -RepetitionDuration $duration;
    "RunLevel" = "Highest";
    "User"     = $credential.UserName;
    "Password" = $credential.GetNetworkCredential().Password;
    "Settings" = $settings;
}

$job1 = @{
    "TaskName" = "2nd one";
    "Action"   = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument "C:\2ndScript;quit";
    "Trigger"  = New-ScheduledTaskTrigger -Once -At 10:00AM -RepetitionInterval $repeat -RepetitionDuration $duration;
    "RunLevel" = "Highest";
    "User"     = $credential.UserName;
    "Password" = $credential.GetNetworkCredential().Password;
    "Settings" = $settings;
}
#[...]
Register-ScheduledTask $job0
Register-ScheduledTask $job1
#[...]
使用这种技术,可以创建和重用模板。这避免了样板和重复

$template = @{
    "TaskName" = "";
    #[...]
    "Settings" = $settings;
}

$job0 = $template.psobject.Copy()
$job1 = $template.psobject.Copy()

$job0.Action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument "MyFirstJob";
$job0.TaskName = "1stOne"
$job1.Action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument "2ndJob";
$job1.Trigger = New-ScheduledTaskTrigger -Once -At 00:00AM -RepetitionInterval $repeat -RepetitionDuration $duration;
#[...] and so on

Register-ScheduledTask @job0
Register-ScheduledTask @job1
#[...]

(新时间跨度-第72天)
?你是对的,应该是(新时间跨度-第3天)。请参阅下面的其他评论。谢谢@Clijsters。我想我没有正确地说明我的问题。我需要能够开始一个sequence的第一个工作将在今天执行,明天下一个和后天第三个任务。感谢您指出以$repeat为单位的小时数。如您所述,它应该更改。我喜欢你转换剧本的方式。我也将使用这种技术。如果我在同一天执行所有任务,则我的备份b/c没有保留策略所有任务都已在同一天或以小时间隔安排。我需要设置开始日期或下一次运行,作业增量之间提前一天。我刚刚使用此配置转换,收到如下消息:命令管道位置1处的内联
cmdlet Register ScheduledTask为以下参数提供值:操作[0]:
已提供$job0.Action,脚本无法完成执行,因此您只想让触发器在明天的处触发?由于
-At
是一个
[datetime]
它可以是
“01.01.2018 4:16:49 PM”
或任何其他已知的和可转换的日期时间,如value@laspalmos:对于您的错误,能否在问题中添加一个编辑部分,提供产生错误的确切代码?老实说,我没有测试我的代码片段。我解决了这个问题,而不是使用$I used@作为变量inline
Register ScheduledTask@job0
,现在它正是我想要的工作方式。感谢您在
[datetime]