使用PowerShell

时间:2017-08-10 09:16:56

标签: windows powershell scheduled-tasks powershell-v4.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 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

如何在触发器中添加天数而不是时间。我感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

准确回答您的问题:

只需将$repeat = (New-TimeSpan -Days 3)替换为$jobname0,即可添加天数而不是时间小时。

添加一些额外信息&提示:

考虑使用here而不是这一大堆变量。使用变量的优点是可重用性。如你所定义,例如$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
#[...]

使用该技术,可以创建和重用模板。这样可以避免样板和重复。

d::DIAGNOSIS