How to use Powershell to create Windows Server backups


How to use Powershell to create Windows Server backupsWe’ve talked about Windows Server Backup feature in a previous article posted on Poweradmin blog. I’ve shown you how to install this feature and how to create and schedule Windows Server backups using the backup console. In this article I want to show you how to achieve similar results by using a Powershell script. I will try to describe each line so you can understand the logic behind it. Note that I use Windows PowerShell Integrated Scripting Environment (ISE) to create Windows scripts.
#We will start by creating a new Windows Backup(WB) policy object:
$backupPolicy = New-WBPolicy
 
#We will add the System State and the Bare Metal Recovery options to our new policy:
Add-WBSystemState -Policy $backupPolicy
Add-WBBareMetalRecovery -Policy $backupPolicy
 
#We’ll need to set a location on where the backups will be stored, I will use a local disk attached to my server. The Get-WBDisk cmdlet displays all disks attached to the server so I’ve used the $disk[1] to specify the needed partition.
$disk = Get-WBDisk
Write-Host $disk
$backupTarget = New-WBBackupTarget -Disk $disk[1]
Add-WBBackupTarget -Policy $backupPolicy -Target $backupTarget
The script will display the backup destination disk information:
Get-WBDisk cmdlet
#We’ll set the Vss Full Backup option to our policy:
Set-WBVssBackupOption -Policy $backupPolicy -VssFullBackup
 
#The backup schedule can be configured using the Set-WBSchedule cmdlet. I’ve set the task to run everyday at 9 am:
Set-WBSchedule -Policy $backupPolicy -Schedule 09:00
#Normally, we would not be allowed to run a one-time only backup this is why we’ll have to force our policy by executing the following command:
Set-WBPolicy -force -policy $backupPolicy
 
#All that’s left to do is to start the backup process:
Start-WBBackup -Policy $backupPolicy
 
Windows Powershell ISEThe backup will now start, Windows Powershell ISE will display a status of the backup process. You will be prompted once the the operation is completed.
Windows Backup ConsoleYou can also use the Windows Backup Console to verify the status of the backup operation. Note that the scheduled task will be configured as specified in the script (everyday at 9 am full VSS backup)
That’s about it for this script folks, hope you will enjoy it when automatizing Windows Server backup tasks. You can further develop the script to run backups for multiple machines at the same time. Note that you can also use a network share as the backup destination. Wish you all the best and stay tuned for the following articles from IT training day.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s