We're done with SQL DB Backup using Powershell
Now my goal is to make it automated and Scheduled, and we can achieve this with 'ScheduledTask' command of PowerShell.
To create schedule task we basically require four main things:
Define an ScheduledTaskAction
Will create ScheduledTaskTrigger
Create NewScheduledTask
Registered Register-ScheduledTask
So let's start practically,
First will write script to generate db_backup with powershell script (click here)
Once done with this now we're going to create scheduledTask by following steps:
1. will initialize variables
$TaskName = "ScheduledDBBackupTask"
$TaskCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$TaskScript = "D:\DB_Backup.ps1"
2. here we're defining an action
$Action = New-ScheduledTaskAction -Execute $TaskCommand -Argument "-NONInteractive -NoLogo -NoProfile -File $TaskScript"
3. here setting up the trigger, (our task will run everyday at 05:35PM)
$Trigger = New-ScheduledTaskTrigger -Daily -At '05:35PM'
4. here creating new scheduledtask
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)
5. task has been created, now it's time to register this newly created task:
$Task | Register-ScheduledTask -TaskName $TaskName
For source code of this script click here
once tasted on your local and if you not required in future then remove scheduledtask by
Unregister-ScheduledTask ScheduedDBBackupTask
To verfiy that your custom scheduled task has been removed:
Get-ScheduledTask
Happy Scripting
Now my goal is to make it automated and Scheduled, and we can achieve this with 'ScheduledTask' command of PowerShell.
To create schedule task we basically require four main things:
Define an ScheduledTaskAction
Will create ScheduledTaskTrigger
Create NewScheduledTask
Registered Register-ScheduledTask
So let's start practically,
First will write script to generate db_backup with powershell script (click here)
Once done with this now we're going to create scheduledTask by following steps:
1. will initialize variables
$TaskName = "ScheduledDBBackupTask"
$TaskCommand = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$TaskScript = "D:\DB_Backup.ps1"
2. here we're defining an action
$Action = New-ScheduledTaskAction -Execute $TaskCommand -Argument "-NONInteractive -NoLogo -NoProfile -File $TaskScript"
3. here setting up the trigger, (our task will run everyday at 05:35PM)
$Trigger = New-ScheduledTaskTrigger -Daily -At '05:35PM'
4. here creating new scheduledtask
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)
5. task has been created, now it's time to register this newly created task:
$Task | Register-ScheduledTask -TaskName $TaskName
For source code of this script click here
once tasted on your local and if you not required in future then remove scheduledtask by
Unregister-ScheduledTask ScheduedDBBackupTask
To verfiy that your custom scheduled task has been removed:
Get-ScheduledTask
Happy Scripting