r/PowerShell • • Jan 08 '20
Schedule Task Template
For those that cannot use the cmdlets for scheduled task but you wish it was cleaner and fed back results. MSI and EXE installs work nice with this template as well. Hope this helps someone out there and gives you an idea on how to cleanup some code or make it easier to read.
$EndTailArgs = @{
Wait = $True
NoNewWindow = $True
ErrorAction = "SilentlyContinue"
ErrorVariable = "+SettingTasks"
PassThru = $True
}
$TaskSchedParams = @{
FilePath = 'SCHTASKS.exe'
ArgumentList = @(
"/Create",
"/SC",
"DAILY",
'/TN "Backup Data"',
'/TR "C:Backup.bat"',
"/ST 07:00"
)
}
$Schedule = Start-Process @TaskSchedParams @EndTailArgs
if($Schedule.ExitCode -eq 0){
"[LastExitCode]:$($Schedule.ExitCode) - has set properly"
} else {
Write-Error -Message "[LastExitCode]:$($Schedule.ExitCode) - has not set properly"
}
34
Upvotes
View all comments
Show parent comment
3
u/AdminAtWork Jan 09 '20
Schtasks.exe can take a remote computer as parameter, so probably run the PowerShell script locally and point schtasks.exe to the remote system.