Welcome to the next installment of our vSphere PowerCLI 5.8 walkthrough series of the new cmdlets for vSphere Storage Policy Based Management. So far we have seen:
Introduction to vSphere Storage Policies
Creating vSphere Storage Policies
Associating vSphere Storage Policies
In this article we will take the next step and illustrate how to leverage vSphere Storage Policies to enhance the provisioning of New VMs. We will have a few provisioning examples involving a virtual machine with a single traditional storage array backed datastore, a vsanDatastore, and a multi-vendor mixed datastore environment.
PowerCLI cmdlets referenced in this blog article:
New-VM
Get-SpbmCompatibleStorage
Get-SpbmEntityConfiguration
Set-SpbmEntityConfiguration
Follow these links for more information on creating vSphere Storage Policies for Virtual SAN:
Using the vSphere Web Client
Using PowerCLI
Get-SpbmCompatibleStorage cmdlet
The Get-SpbmCompatibleStorage cmdlet performs datastore compatibility checking for virtual machines based on a vSphere Storage Policy. When executed, this cmdlet returns a list of datastores matching the specified vSphere Storage Policy. If the compatibility checking for a datastore does not produce any errors, the datastore is considered as a viable candidate for virtual machine file storage.
When the CandidateStorage parameter is specified, each object in the output is equal to some of the objects in the input (the -eq operation returns true).
Syntax
Get-SpbmCompatibleStorage -StoragePolicy <SpbmStoragePolicy> [-CandidateStorage <StorageResource[]>] [-Server <VIServer[]>] [<CommonParameters>]
Note: The PowerCLI 5.8 cmdlets for vSphere Storage Policy Based Management are supported for vCenter Server 5.5 or later.
Cmdlet Parameters
NAME | TYPE | DESCRIPTION | REQUIRED? | PIPELINE INPUT | DEFAULT VALUE |
CandidateStorage | StorageResource[] | Specifies a list of datastores and storage pods that are candidates for storage resources. If you do not specify this parameter, the server uses all of the datastores and storage pods for placement compatibility checking. | FALSE | true (ByValue) | |
Server | VIServer[] | Specifies the vCenter Server systems on which you want to run the cmdlet. If no value is given to this parameter, the command runs on the default servers. For more information about default servers, see the description of Connect-VIServer. | FALSE | FALSE | |
StoragePolicy | SpbmStoragePolicy | Specifies a storage requirement policy. | TRUE | true (ByValue) |
Examples
1. Returns all datastores and datastore clusters that are in compliance with the specified vSphere Storage Policy.
Get-SpbmCompatibileStorage -StoragePolicy $policy
2. Returns all datastores and datastore clusters from the $ListOfDatastoreOrDatastoreCluster list that are in compliance with the $policy storage policy.
Get-SpbmCompatibileStorage -StoragePolicy $policy -CandidateStorage $ListOfDatastoreOrDatastoreCluster
PowerCLI provisioning examples
Single Datastore Environment
Previously we would use the following PowerCLI command string to create our new VM:
New-VM -Name VM1 -VM WinTemplate1 -Datastore “Datastore1” -VMHost esx1.vmware.com
In the example above, the “-Datastore” property denotes a specific datastore for our new VM to be placed onto. In a single-datastore environment this command completes the desired action of initiating the creation of a new virtual machine with its placement on Datastore1.
Single Virtual SAN Datastore Environment
In an environment containing a single datastore that is a Virtual SAN Datastore, we can direct virtual machine storage placement by specifying the “vsanDatastore” value in the Datastore property of the New-VM cmdlet. Upon execution, this command string will create the new virtual machine on our Virtual SAN datastore.
New-VM -Name VM1 -VM WinTemplate1 -Datastore “vsanDatastore” -VMHost esx1.vmware.com
Our Virtual SAN example is different from our previous example in that Virtual SAN leverages vSphere Storage Policy Based Management for all virtual machines provisioned on the vsanDatastore. The default vSphere Storage Policy for the vsanDatastore contains the following values:
We can use the new “Set-SpbmEntityConfiguration” PowerCLI cmdlet to specify a specific vSphere storage policy with this virtual machine rather than the default policy.
Get-VM -Name VM1 | Set- SpbmEntityConfiguration –StoragePolicy GoldPolicy
Multi-Mixed Datastore Environment
In an environment with multiple storage arrays from multiple vendors, along with siloed storage and vSphere administration roles, the provisioning process can become quite complicated, very quickly. Typically in larger environments, the vSphere administrator will often need to communicate with the storage administrator in order to identify the proper storage for placement of the virtual machine. The storage administrator is tasked with keeping track of storage capabilities, status, etc to communicate back to the vSphere administrator, which often results in a multitude of complex spreadsheets to track storage configurations, consumption and can impose considerable added time to the provisioning process.
With the new PowerCLI 5.8 cmdlets for vSphere Storage Policy Based Management, we are now able to automate the storage selection process entirely within PowerCLI. After assigning metadata-tags to non Virtual SAN datastores, we can then create vSphere Storage Policies that reflect the capabilities and metadata-tags of the various storage arrays in the environment. We can then use the new Get-SpbmCompatibleStorage cmdlet in conjunction with the New-VM cmdlet to add a robust datastore selection mechanism into our scripts.
The examples below each clone the base-sles-01a virtual machine template to create a virtual machine
Single-Line
New-VM -Name VM1 -VM base-sles-01a -Datastore ((Get-SpbmCompatibleStorage -StoragePolicy GoldPolicy)[0]) -VMHost esx-01a.corp.local
Multi-line
$VM = “VM1” $Template = “WinTemplate1” $VMHost = “esx1.vmware.com” $StoragePolicy = “GoldPolicy” $Datastore = (Get-SpbmCompatibleStorage -StoragePolicy “$StoragePolicy”) New-VM -Name $VM -VM $WinTemplate1 -Datastore $Datastore[0] -VMHost $VMHost
Output
Note: The command string “Get-SpbmCompatibleStorage -StoragePolicy GoldPolicy” will output a list of compatible datastores in the form of a Powershell array. Arrays provide a great way to store multiple values or objects within a single variable.
The New-VM cmdlet is looking for a single datastore, rather than an array possibly listing multiple datastores, to place the new virtual machine upon though. If the Get-SpbmCompatibleStorage cmdlet returns only 1 datastore the New-VM cmdlet will complete successfully. If multiple values are returned in the array, we can select the first compatible datastore by referencing the first item in the array. To select the first item in the array simply add “[0]” to the end of the $Datastore variable or Get-SpbmCompatibleStorage command string as in the example below.
$Datastore = (Get-SpbmCompatibleStorage -StoragePolicy “$StoragePolicy”)[0]
For more information on accessing values within an array, see the following link:
Microsoft Technet: Accessing Values in an Array (Powershell)
Associate a vSphere Storage Policy with the new virtual machine
After creating the virtual machine, simply associate the storage policy with the virtual machine (or individual virtual disks) and the provisioning workflow is complete. Use the Set-SpbmEntityConfiguration cmdlet to apply a vSphere Storage Policy to the virtual machine and the Get-SpbmEntityConfiguration cmdlet to validate the association.
See this article for more information on Associating vSphere Storage Policies.
That completes our demonstration of how to leverage vSphere Storage Policies to automate the storage selection process of our virtual machine provisioning workflows. In our next article, we will begin to look at SLA compliance and reporting capabilities using the new PowerCLI 5.8 cmdlets for vSphere Storage Policy Based Management. If you have any feedback or requests for this series, please do not hesitate to email [email protected] and I will poll the top requested items and include in this series.
Resources
- vSphere Storage Policy Based Management Overview – Part1
- vSphere Storage Policy Based Management Overview – Part 2
- vSphere PowerCLI 5.8 SPBM Walkthrough – Part 1: Introduction
- vSphere PowerCLI 5.8 SPBM – Part2: Creating vSphere Storage Policies
- vSphere PowerCLI 5.8 SPBM – Part3: Associating vSphere Storage Policies
- Download and Install PowerCLI 5.8 Release 1
- Download and Install PowerCLI for vCD and vCloud Air Tenants 5.8 Release 1