Using PowerShell for bare-metal recovery

Brien Posey shows you how to create backup in PowerShell that is suitable for performing a bare-metal recovery.

Over the last couple of versions of Windows Server, Microsoft has worked to make PowerShell ever more capable. One of the somewhat surprising things that you can do with PowerShell in Windows Server 2008 R2 and Windows server 2012 is create a backup that is suitable for performing a bare-metal recovery. In this article, I will show you how.

While you don't have to use PowerShell to create a bare-metal backup, Microsoft has stated that PowerShell is the preferred administrative interface for Windows Server and that GUI-based management will eventually become a thing of the past. Admins need to learn PowerShell now to avoid getting left behind.

Recovery operations are backup application-specific. This article is based around the use of the native Windows Server Backup application. There are other backup vendors that support command-line operations, but the required command set will vary from one vendor to the next.

With that said, you will have to prepare Windows Server by running the following commands in order to add the Windows Server Backup command set to PowerShell:

Add-PSSnapin Windows.ServerBackup

Windows Server 2012 will automatically load the required PowerShell modules, but if you are using Windows Server 2008 R2, then you need to use the Import All Modules command to load the required modules.

Adding bare metal recovery capabilities

The Windows Backup Policy dictates whether or not you can create or restore bare-metal recovery capable backups. Therefore, you must begin by determining whether not such a policy exists. To do so, enter the Get-WBPolicy cmdlet.

If you find that no Windows Backup Policy exists, then you will have to create one. To do so, enter the -WBPolicy cmdlet.

Once a Windows Backup policy has been created, it must be modified in order to allow for bare-metal recovery. To make this modification, you must be a member of either the Administrators group or the Backup Operators group. Furthermore, the policy must be marked as editable before you can modify it.

If you just created a Windows Backup policy using the -WBPolicy cmdlet, then the policy should already be flagged as editable. If not, then make it editable by entering the Get-WBPolicy cmdlet and appending the –Editable parameter.

Once the Windows Backup policy has been flagged as editable, you can add bare-metal recovery capabilities to the policy by using the Add-WBBareMetalRecovery cmdlet. When doing so, you will also need to append the –Policy attribute and the variable that represents your Windows Backup Policy. Typically you should be able to use the following command:

Add-WBBareMetalRecovery –Policy $policy

After adding bare-metal recovery capabilities to the Windows Backup policy, it is a good idea to verify that the operation was successful. The easiest way to do so is to use the Get-WBBareMetalRecovery cmdlet to view whether or not the policy has been provisioned for bare-metal recovery. The actual command used for doing so is:

Get-WBBareMetalRecovery –Policy $policy

Once you've created the Windows Backup policy, using it is surprisingly easy. You can run a one-off backup by using the Start-WBBackup cmdlet. When you do, simply tell Windows that you want to use the previously created policy. 

You will also typically need to specify a backup location, which is usually done by assigning a backup target to a variable by way of the -WBBackupTarget cmdlet. After doing so, the Add-WBBackupTarget cmdlet is used to assign the specified backup target to the policy that you created earlier. For example, you might use the following commands to designate a backup target:

$BackupLocation = =WBBackupTarget –VolumePath E:
Add-WBBackupTarget –Policy $Policy –Target $BackupLocation

Once you've specified a backup target, you can launch the backup process. To do so, you could use the following command:

Start-WBBackup –Policy $policy

Although this command manually launches a backup, it is just as easy to create a scheduled backup.

Putting it all together

Although I have explained how the various commands are used, I thought it might be helpful to provide you with a series of commands for creating a Windows Backup policy, provision it for bare-metal recovery and launch a backup. With that said, the following commands can be used:

$policy = -WBpolicy
Add-WBBareMetalRecovery $policy
$BackupLocation = -WBBackupTarget –VolumePath E:
Add-WBBackupTarget –Policy $Policy –Target $BackupLocation
Start-WBBackup –Policy $policy

In this article, I have shown you how to use PowerShell to create a backup that is suitable for bare-metal recovery. You can use similar techniques for other types of backups as well. Cmdlets such as Add-WBVolume, Add-WBFileSpec and Add-WBSystemState can be used to create other types of backups.

About the author:
Brien M. Posey, MCSE, has received Microsoft's MVP award for Exchange Server, Windows Server and Internet Information Server. Brien has served as CIO for a nationwide chain of hospitals and has been responsible for the department of information management at Fort Knox. You can visit Brien's personal website at www.brienposey.com.

Next Steps

Using PowerShell to create data backups

Dig Deeper on Data backup and recovery software

Disaster Recovery