VMware has done a lot to open up the APIs for vCloud with the 5.1 release, however it still leaves much to be desired. One of the nicer things is the ability to change a storage profile for a VM. However, you need to know the HREF for the storage profile that you want to change to. This wasn’t so easy to get (I would love to be able to use a “get-storageProfile” PowerCLI cmd-let), but thankfully, Jake Robinson (@jakerobinson) and the VMware Community to the rescue:
http://communities.vmware.com/message/2163559#2163559
This script uses PowerCLI for Tenants (which cannot be installed on the same box running the ‘regular’ PowerCli). Taking his prompt to build an XML file from an HTTP GET to a vCloud HREF, we can retrieve storage profiles from any OrgvDC you have rights to. From this XML, we can assign a storage profile to a VM (or in this case, every VM in a vApp) based on it’s name and the Org you’re logged into. I modified his script a little bit, because if we pass an Org to the function, we don’t get the storage profiles, but if we pass an OrgvDC HREF, we automatically get the storage profiles (because storage profiles are assigned to Org vDCs and not globally to an Org). This reduces the number of function calls needed.
All this script needs is your vApp name and desired Storage Profile name.
What this also addresses is the ability to migrate all vCloud VMs off of the “*Any” Storage Profile.
# This function does a HTTP GET against the vCloud 5.1 API using our current API session. # It accepts any vCloud HREF. function Get-vCloud51($href) { $request = [System.Net.HttpWebRequest]::Create($href) $request.Accept = "application/*+xml;version=5.1" $request.Headers.add("x-vcloud-authorization",$global:DefaultCIServers[0].SessionId) $response = $request.GetResponse() $streamReader = new-object System.IO.StreamReader($response.getResponseStream()) $xmldata = $streamreader.ReadToEnd() $streamReader.close() $response.close() return $xmldata } # This function gets an OrgVdc via 1.5 API, then 5.1 API. # It then returns the HREF for the storage profile based on the $profilename and function Get-storageHref($orgVdc,$profileName) { $orgVdc51 = Get-vCloud51 $orgVdc.Href $storageProfileHref = $orgVdc51.vdc.VdcStorageProfiles.VdcStorageProfile | Where-Object{$_.name -eq "$profileName"} | foreach {$_.href} return $storageProfileHref } # Get vApp, Storage Profile and OrgvDC names $vappName = read-host "vApp name" $profileName = read-host "Storage Profile" $orgVdcName = read-host "Org vDC Name" $orgVdc = get-orgvdc $orgVdcName #Get storage profile HREF $profileHref = Get-storageHref $orgVdc $profileName # Change each VM's Storage Profile in the vApp $CIvApp = Get-CIVApp $vappName Foreach ($CIVM in ($CIvApp | Get-CIVM)) { $newSettings = $CIVM.extensiondata $newSettings.storageprofile.name = "$profileName" $newSettings.storageprofile.Href = "$profileHref" Write-Host "Changing the storage profile for $CIVM.name to $profileName" $newSettings.UpdateServerData() }