{"id":3612,"date":"2016-12-19T17:16:49","date_gmt":"2016-12-19T23:16:49","guid":{"rendered":"http:\/\/www.jasemccarty.com\/blog\/?p=3612"},"modified":"2020-02-16T19:45:13","modified_gmt":"2020-02-17T01:45:13","slug":"spbm-backup-recover-w-powercli","status":"publish","type":"post","link":"https:\/\/www.jasemccarty.com\/blog\/spbm-backup-recover-w-powercli\/","title":{"rendered":"Backup or Recover SPBM Profiles with PowerCLI"},"content":{"rendered":"<p>I work in the lab a lot.<\/p>\n<p>One of the tasks that I often perform is:<\/p>\n<ul>\n<li>Blowing away my VCSA (or Windows vCenter Server),<\/li>\n<li>Redeploying it, and<\/li>\n<li>Reattaching it to a cluster that is running vSAN.<\/li>\n<\/ul>\n<p>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.<\/p>\n<p><a href=\"http:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/NoPolicies.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3641\" src=\"http:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/NoPolicies-300x100.png\" width=\"600\" height=\"200\" srcset=\"https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/NoPolicies-300x100.png 300w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/NoPolicies-768x257.png 768w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/NoPolicies.png 1021w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><!--more--><strong>PowerCLI cmdlets<\/strong><br \/>\nThere is a PowerCLI cmdlet that will allow administrators to export SPBM policies. Basically the cmdlet is &#8220;<em>Export the policy named ~whatever~ and put it in X folder<\/em>&#8221;<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Export-SpbmStoragePolicy -StoragePolicy $policy -FilePath 'folder\\file path'<\/pre>\n<p>The named policy is exported as an xml\u00a0file that looks something like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3617\" src=\"http:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM-XML-300x233.png\" width=\"600\" height=\"465\" srcset=\"https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM-XML-300x233.png 300w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM-XML-768x595.png 768w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM-XML-1024x794.png 1024w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM-XML.png 1108w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>The Import-SpbmStoragePolicy\u00a0PowerCLI cmdlet allows administrators to import the SPBM xml.<\/p>\n<pre class=\"brush: powershell; title: ; wrap-lines: true; notranslate\" title=\"\">Import-SpbmStoragePolicy -Name policyname -Description description -FilePath 'folder\\file.xml'<\/pre>\n<p>These 2 cmdlets make it easy to download\/upload an SPBM policy xml file.<\/p>\n<p><strong>What about multiple policies?<\/strong><br \/>\nBut what if you have many policies? These cmdlets work <em>one policy at a time<\/em>.<\/p>\n<p>To export many policies, we&#8217;d need to enumerate the policies in vCenter.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Get a list of all the storage policies on the specified vCenter Server\r\n $StoragePolicies = Get-SpbmStoragePolicy -Server $Server\r\n \r\n # Enumerate the list of storage policies\r\n Foreach($StoragePolicy in $StoragePolicies) {\r\n \r\n# Get the name of the policy that is being exported.\r\n$PolicyName = $StoragePolicy.Name\r\n\r\nExport-SpbmStoragePolicy -FilePath $Path -StoragePolicy $StoragePolicy.Name -Server $Server\r\n }<\/pre>\n<p>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.<\/p>\n<p>Grabbing all the files in a path would look something like this:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">$PolicyFiles = Get-ChildItem $FilePath -Filter *.xml<\/pre>\n<p>To handle each of them, we can use Foreach again.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Foreach ($PolicyFile in $PolicyFiles)<\/pre>\n<p>To see what each file contains, we&#8217;ll need to parse it.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Get the PolicyFile Path\r\n$PolicyFilePath = $PolicyFile.FullName\r\n\r\n# Get the contents of the file\r\n$xml = &#x5B;xml](Get-Content $PolicyFilePath)\r\n\r\n# Grab the name of the policy so it may be set properly in vCenter\r\n$PolicyName = $xml.PbmCapabilityProfile.Name.'#text'\r\n\r\n# Grab the description of the policy so it may be set properly in vCenter\r\n$PolicyDescription = $xml.PbmCapabilityProfile.Description.'#text'\r\n\r\n# Import the policy\r\nImport-SpbmStoragePolicy -Name $PolicyName -Description $PolicyDescription -FilePath $PolicyFile<\/pre>\n<p><strong>The Result<\/strong><br \/>\nAfter a little cleanup and error handling,\u00a0the resulting script can allow the backup and recovery\u00a0of storage policies into a new vCenter instance.<\/p>\n<p><a href=\"http:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3639\" src=\"http:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM2-300x173.png\" width=\"600\" height=\"346\" srcset=\"https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM2-300x173.png 300w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM2-768x443.png 768w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM2-1024x590.png 1024w, https:\/\/www.jasemccarty.com\/blog\/wp-content\/uploads\/2016\/12\/SPBM2.png 1600w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>The completed script can be found on the VMware Developer site at the following URL:<br \/>\n<a href=\"https:\/\/developercenter.vmware.com\/samples?id=1661\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/developercenter.vmware.com\/samples?id=1661<\/a><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":3639,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135,10,12,168],"tags":[53,207,65,167,87,152],"class_list":["post-3612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-defined","category-storage-2","category-virtualization","category-vsan","tag-lab","tag-policies","tag-powercli","tag-spbm","tag-storage","tag-vsan"],"_links":{"self":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/3612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/comments?post=3612"}],"version-history":[{"count":41,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/3612\/revisions"}],"predecessor-version":[{"id":4100,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/3612\/revisions\/4100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/media\/3639"}],"wp:attachment":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/media?parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/categories?post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/tags?post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}