1

I am working on script which will execute on next day and then will run every other 1,2 and 3 day(s). Here is my script:

#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

How I can add days instead of time into the trigger. I appreciate all your help.

2
  • 2
    (New-TimeSpan -Days 72 ) ?? Commented Aug 10, 2017 at 10:46
  • You are right it should be (New-TimeSpan -Days 3). Please see additional comments below. Commented Aug 10, 2017 at 12:50

1 Answer 1

2

To answer your question precisely:

Just replace $repeat = (New-TimeSpan -Hours 72 ) with $repeat = (New-TimeSpan -Days 3) and you've added days instead of time Hours.


To add some additional information & tips:

Consider using Splatting instead of this big bunch of variables. The advantage of using variables is reusability. As you define e.g. $jobname0 only to use it as parameter, it has (nearly) no advantage.

Your code could look like this

$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
#[...]

Using that technique, one is able to create and reuse templates. This avoids boilerplate and duplicates.

$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
#[...]
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you @Clijsters. I think I didn't specify my question correctly. I need to be able start task in a seuquence where first job will be executed today, next one tomorrow and third one the day after tomorrow. Thank you for pointing out the hours in $repeat it should be changed as you mentioned. I love the way you converted this script. I will use this technique as well. If I execute all task same day there is no retention policy for my backup b/c all task has been scheduled same day or with hours interval. I need to setup start date or next run with advance of one day between jobs increments.
I just converted with this configuration and I am getting message as shown below: inline cmdlet Register-ScheduledTask at command pipeline position 1 Supply values for the following parameters: Action[0]: The $job0.Action has been provided and script is unable finish the execution
So you simply want to let the Trigger to be -At Tomorrow? As -At is a [datetime] it can just be "01.01.2018 4:16:49 PM" or any other known and convertible datetime-like value
@laspalmos: For your error, could you add an EDIT-section to your question providing the exact code, which produces the error? To be honest, I didn't test my snippet.
I resolved the issue instead of using $ I used @ for the variable inlineRegister-ScheduledTask @job0 Now it works exactly how I wanted. Thank you for the hint abut the [datetime]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.