April 19, 2024

Backup or Recover SPBM Profiles with PowerCLI

I work in the lab a lot.

One of the tasks that I often perform is:

  • Blowing away my VCSA (or Windows vCenter Server),
  • Redeploying it, and
  • Reattaching it to a cluster that is running vSAN.

As a result of a new VCSA deployment, only a few SPBM policies will be present. Any previously available SPBM polices are lost with the previous VCSA instance.

PowerCLI cmdlets
There is a PowerCLI cmdlet that will allow administrators to export SPBM policies. Basically the cmdlet is “Export the policy named ~whatever~ and put it in X folder

Export-SpbmStoragePolicy -StoragePolicy $policy -FilePath 'folder\file path'

The named policy is exported as an xml file that looks something like this:

The Import-SpbmStoragePolicy PowerCLI cmdlet allows administrators to import the SPBM xml.

Import-SpbmStoragePolicy -Name policyname -Description description -FilePath 'folder\file.xml'

These 2 cmdlets make it easy to download/upload an SPBM policy xml file.

What about multiple policies?
But what if you have many policies? These cmdlets work one policy at a time.

To export many policies, we’d need to enumerate the policies in vCenter.

# Get a list of all the storage policies on the specified vCenter Server
 $StoragePolicies = Get-SpbmStoragePolicy -Server $Server
 
 # 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 $Path -StoragePolicy $StoragePolicy.Name -Server $Server
 }

Importing many policies would work the same way, but each policy would have to be passed to the cmdlet. The path and xml policy file names could be part of an array, or read directly from a directory.

Grabbing all the files in a path would look something like this:

$PolicyFiles = Get-ChildItem $FilePath -Filter *.xml

To handle each of them, we can use Foreach again.

Foreach ($PolicyFile in $PolicyFiles)

To see what each file contains, we’ll need to parse it.

# Get the PolicyFile Path
$PolicyFilePath = $PolicyFile.FullName

# Get the contents of the file
$xml = [xml](Get-Content $PolicyFilePath)

# Grab the name of the policy so it may be set properly in vCenter
$PolicyName = $xml.PbmCapabilityProfile.Name.'#text'

# Grab the description of the policy so it may be set properly in vCenter
$PolicyDescription = $xml.PbmCapabilityProfile.Description.'#text'

# Import the policy
Import-SpbmStoragePolicy -Name $PolicyName -Description $PolicyDescription -FilePath $PolicyFile

The Result
After a little cleanup and error handling, the resulting script can allow the backup and recovery of storage policies into a new vCenter instance.

The completed script can be found on the VMware Developer site at the following URL:
https://developercenter.vmware.com/samples?id=1661

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.