Task Scheduled Backup with Powershell
Mattia Grandi
Active Directory Migration | Senior Identity Consultant | Powershell Enthusiast | M&A Consultant
In an era in which everything, or almost everything, needs to be automated, a scheduler is a tool of fundamental importance.
Unfortunately, not all realities allow to have and use advanced third party schedulers, therefore we often refer to the good old "Task Scheduler" in 微软 environment.
You create scripts, or simple actions, setup the scheduling and leave them to run their course... until the machine has some problem or get corrupted following a bad shutdown.
Good backup practices certainly come to our rescue, but why not make a convenient backup of the scheduler alone instead of the whole machine?
With this article I will explain my method!
Unfortunately there is no out-of-the-box way to back up all the tasks we have scheduled, but Powershell can help us, and do automatically what we would otherwise have to do manually exporting each task every time, with a great waste of time.
By simply looping through all the scheduled tasks, suitably filtered, it is possible to export them in XML format, zip them and possibly send via email or store in a network share.
领英推è
# Script to export the settings of all the custom scheduled tasks #
# Mattia Grandi - 30/3/2021 #
# Script version: 1.0 #
# Setup the Backup Folder and setup the paths#
$backup = "C:\Backups"
IF (!(Test-Path $backups)) {New-Item -ItemType Directory -Path "C:\" -Name "Backups"}
$outcsv = "$backup\ScheduledTask.csv"
$outxml = "$backup\xml"
IF (Test-path $outcsv) {
? ? Remove-Item $outcsv
? ? Remove-Item $outxml\*
? ?}
# Export all the scheduled tasks name on this machine #
# /FO switch indicates the format #
$TaskList = schtasks.exe /query /s localhost? /V /FO CSV | ConvertFrom-Csv
# Create a match filter that exclude the System tasks
$Match = Get-ScheduledTask -TaskPath "\" | ? {($_.Taskname -notmatch "S-1-5")}
# Loop into all the filtered scheduled tasks #
Foreach ($m in $match)
{
? ? # Export the task in xml format #
? ? Export-ScheduledTask $M | Out-File "$outxml\$($M.taskname).xml"
? ? # Create a custom export result with only needed informations #
? ? # then export it as csv #
? ? $Result = $null
? ? $Result = $Tasklist | ? Taskname -Match $m.taskname?
? ??
? ? $export = [pscustomobject]@{
? ? ? ??
? ? ? ? Taskname = $Result.taskname
? ? ? ? Status = $Result.Status
? ? ? ? LogonMode = $Result.'Logon Mode'
? ? ? ? Author = $Result.Author
? ? ? ? TaskToRun = $Result.'Task to run'
? ? ? ? StartIn = $Result.'Start In'
? ? ? ? Comment = $Result.Comment
? ? ? ? State = $Result.'Scheduled Task State'
? ? ? ? ServiceUser = $Result.'Run As User'
? ? ? ? Options = $Result.'Delete Task If Not Rescheduled'
? ? ? ? StopTask = $result.'Stop Task If Runs X Hours and X Mins'
? ? ? ? ScheduleType = $Result.'Schedule Type'
? ? ? ? StartTime = $Result.'Start Time'
? ? ? ? ScheduleDays = $Result.Days
? ? ? ? ScheduleMonths = $result.Months
? ? ? ? Repeat = $result.'Repeat: Every'
? ? ? ? Duration = $Result.'Repeat: Until: Duration'
? ? ? ? StopIfRunning = $Result.'Repeat: Stop If Still Running'
? ? ? ? } | Export-csv $outcsv -NoTypeInformation -Append -Delimiter ";"
}
At this point, in our C: \ Backups folder we will find a csv file containing all the scheduled tasks we created with the information we need, and a folder containing all the tasks exported in xml format.
Adding the following lines will allow you to Zip the content of the xml export folder and sent it to a predefined email address or distribution list:
Compress-Archive -Path $outxml -DestinationPath $backup\TaskExport?
$Attachment = "$backup\TaskExport.zip"
$SMTP = "smtp.yourdomain.com"
$To = "backups@yourdomain.com"
$From = "Task Scheduler Backup <chdbkp@yourdomain.com>"
$Subject = "Task Scheduler backup from $("$env:computername.$env:userdnsdomain")"
$Body = $match | ConvertTo-Html -Property TaskName | Out-String
Send-MailMessage -to $To -from $From -SmtpServer $SMTP -Subject $Subject -Body $Body -BodyAsHtml -Attachments $Attachment?
After saving your script as TaskSchedulerBackup.ps1 and schedule it to run once a day, you will have a full readable and ready backup of your scheduled tasks!
Senior IT Consultant at Nixar AB
2 å¹´Nice job! it helped :)