Skip to content

Instantly share code, notes, and snippets.

@pferreirafabricio
Last active December 6, 2020 00:15
Show Gist options
  • Save pferreirafabricio/f4b7e2147d46b024bf2a9cb8ccaafc65 to your computer and use it in GitHub Desktop.
Save pferreirafabricio/f4b7e2147d46b024bf2a9cb8ccaafc65 to your computer and use it in GitHub Desktop.
Create a scheduled task that will run a PowerShell script at 1:00 AM, in this example a script that will make a backup of a MongoDB Database
$scriptToExecutePath = "C:\Path\To\dump-script.ps1";
$taskName = "MongoDB Backup - <databaseName>"
$taskDescription = "Make a dump of <databaseName> database"
$action = New-ScheduledTaskAction `
-Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' `
-Argument "-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File $scriptToExecutePath"
$trigger = New-ScheduledTaskTrigger -Daily -At 1am
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8
Register-ScheduledTask `
-TaskName $taskName `
-Action $action `
-Trigger $trigger `
-Settings $taskSettings `
-Description $taskDescription
# OBS: If you need to run the task manually for testing use:
# Start-ScheduledTask -TaskName 'MongoDB Backup - <databaseName>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment