Friday, July 8, 2016

Assigning a Storage Policy to Multiple VMs with PowerCLI

Storage Policy-Based Management (SPBM) is one of the most compelling benefits of Virtual SAN. With SPBM, you can define a number of policies each with a rule-set that governs items such as availability and performance. A policy can then be assigned to existing virtual machines and when creating a new virtual machine. It is even possible to assign a policy to individual virtual disks. The ability to assign policies at the virtual machine and virtual disk levels enables precise management of storage without having to create LUNs, define RAID sets, mask LUNs, etc. There are cases where an administrator might need to assign a policy to a group of virtual machines. This article provides a short script for automating the assignment of a storage policy to multiple virtual machines using VMware vSphere PowerCLI.

A new Virtual SAN storage policy is created that defines the Failure Tolerance Method as RAID-5/6 (Erasure Coding) and the Number of Failures to Tolerate = 1. I will call this new policy "R5EC". There are a number of existing virtual machines on my Virtual SAN datastore, which is named "vsanDatastore". I want to assign the new R5EC policy to all of the virtual machines on my Virtual SAN datastore. Here is the PowerCLI code I used to accomplish this...

connect-VIServer <IP or FQDN> -Protocol https -User <user@domain> -Password <password>
$dataStores = "vsanDatastore"
$policy = "R5EC"
# Modify the Name parameter for specific groups of VMs, e.g., -Name app* for all VMs with names that start with app
$vms = Get-VM -Name * -Datastore $dataStores
# Assign the $policy storage policy to the $vms virtual machines and hard disks.
foreach ($VM in $vms) { $VM, (Get-HardDisk -VM $VM) | Set-SpbmEntityConfiguration -StoragePolicy $policy }

As the policy is applied, you will see output for each of the virtual machines (VM Home and VMDK objects) in the VMware vSphere PowerCLI window. It takes a few seconds for each object. If you have a large number of virtual machines, it could take a while to get through the whole list, but this is much better that changing policies one virtual machine at a time through the UI.

@jhuntervmware on Twitter

1 comment:

Note: Only a member of this blog may post a comment.