How to Backup or Recover SPBM with PowerCLI

We have discussed about the SPBM  and how to create SPBM on previous posts , today we are going to discuss about how can backup and restore the SPBM from Power CLI  .

Why we need backup or restore operation on SPBM 

Just imagine once the case we have to redeploy our vCenter which is coiffured with vSAN  and there are many policy . In such cases power CLI is the best tool to perform backup and restore of SPBM .

You can use below commands to perform required tasks

Check the Available Policy 

# Get-SpbmStoragePolicy

I  have created and using two policies on my vSAN environment New Test Policy and New ISCSI Test Policy

# Get-SpbmStoragePolicy -Name “Policy Name”

Check the Policy Detail with specific name which we provide

# Get-SpbmStoragePolicy  | Format-List

Get the Options available with Get-SpbmStoragePolicy Command

#Get-SpbmStoragePolicy -Id "PolicyId"

Listing policy with policy ID which we found from above command

#Get-SpbmStoragePolicy -Requirement -Namespace “VSAN”

Returns all storage policies of type Requirement containing one or more rules which contain one or more capabilities from the “VSAN” namespace

Export the Policy 

# Export-SpbmStoragePolicy -StoragePolicy $policy -FilePath ‘folder\file path’

Exported File Looks like below

Exports a storage policy to the policy.xml file in location . If a file with the same name already exists in this location, the file is overwritten. And if you do not use the Force parameter, the command returns an error about the existing file.

Import Policy 

#Import-SpbmStoragePolicy -Name "Policy Name" -Description "PolicyDescription" -FilePath C:\policy.xml

Imports a storage policy with your name " Policy Name" and description "PolicyDescription" from the policy.xml file to a server. 
And new storage policy is created on the server.

Export All Policy

If you want to export all the policy to specific folder  , you can follow below power shell script .


# Get a list of all the storage policies on the specified vCenter Server
$StoragePolicies = Get-SpbmStoragePolicy

# Enumerate the list of storage policies
Foreach($StoragePolicy in $StoragePolicies) {

# Get the name of the policy that is being exported.
$PolicyName = $StoragePolicy.Name

Export-SpbmStoragePolicy -FilePath C:\Users\Administrator\Desktop\POLICY\ -StoragePolicy $StoragePolicy.Name
}


Note :- This Script will work only for after connecting to vCenter from power CLI else you have add commands to connecting to vCenter to this script .

Reference

Script for export and import can be found on the VMware Developer Site