VMware Cloud Community
Kiil
Contributor
Contributor

Question: sVmotion and vCloud Director - updating VM's storage profile

Hi,

I'm going to migrate a lot of vApps/virtual machines running in vCloud Director to a new storage system, and have created new Storage Profiles for this...

I've found info and tried out some alterntives, and one that _almost_ works is the script below.  But, the virtual machines storage profile is not updated accordingly.  Could some please look through the script and maybe point me in the right direction to get this right ?

The environment I'm trying to run this in runs:

vCloud Director 5.1

vCenter 5.0 U2

ESXi 5.0 U2

You will notice that I've commented out some commands.  This because this doesn't work here...this is related to profiles! 

########################################################################################################

# 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}

write-host $storageProfileHref

return $storageProfileHref

}

#################################################################################################################################################

$orgVdc = get-orgvdc "Trainee vDC"

$profileName = "SystemDisk"

$profileHref = Get-storageHref $orgVdc $profileName

$destDatastoreName = "R01A-VCLOUD-SYS-CNN-01"

$vmNames = $orgVdc | get-civapp get-civm Alexandra | get-civm

foreach($vmName in $vmNames)

{

    $vmQuery = Search-Cloud -QueryType AdminVM -Name $vmName

     if ($destDatastoreName -eq $vmQuery.datastoreName)

     {

      Write-Host -ForegroundColor Red "Silly Wabbit, You are trying to sVmotion to the same Datastore."

      #break

     }

     else

     {

         

     if($vmName.Status -match "PoweredOn")

     {    

        write-host Suspending $vmName.name

        $vmName | suspend-civm

       

     }

     if($vmName.Status -match "PowerOff" -or $vmName.Status -match "Suspended")

     {

        write-host No need to suspend $vmName.name, just going on!

     }    

     $dsQuery = Search-Cloud -QueryType Datastore -Name $destDatastoreName

     $dsRef = New-Object vmware.vimautomation.cloud.views.reference

     $dsRef.Href = "https://$($global:DefaultCIServers[0].name)/api/admin/extension/datastore/$($dsquery.id.split(':')[-...)"

     write-host Relocating $vmName

     $vmName.ExtensionData.Relocate($dsRef)    ## This works, but the virtual machines profile name doesn't get updated

    

     #$newSettings = $vmName.ExtensionData

     #$newSettings.storageprofile.name = "$profileName"   ## This doesn't work - does $vmName.ExtensionData.Storageprofile exist ?

     #$newSettings.storageprofile.Href = "$profileHref"   ## This doesn't work - does $vmName.ExtensionData.Storageprofile exist ?

     #Write-Host "Changing the storage profile for $vmName to $profileName"

    

    

     $vmName | start-civm

     #$newSettings.UpdateServerData()

    }

}

Bjørn-Ove Kiil
0 Kudos
0 Replies