6 Replies Latest reply on Dec 26, 2012 11:54 PM by LucD

    Powercli Storage VMotion

    nsusa Novice

      I have a large number of VMs with 3 different hard drives each. Each VMDK sits on a different LUN. Due to issues on the storage side I need to evacuate all VMs from the first LUN, while the other 2 LUNs do not need to be touched. The problem is that the LUN in question not only holds a VMDK for a VM, but that is also the LUN for all VM related system files (configuration, logs, etc.). I was able to script a Storage Vmotion process, but that only moves the VMDK - not the other pieces of the VM.



      $vms =  Import-Csv "C:\scripts\vms.csv"
      $cloneTask = foreach ($customer in $vms) { Get-HardDisk -vm $customer.name  | Where {$_.Name -eq "Hard disk 1"} | % {Set-HardDisk -HardDisk $_ -Datastore $customer.lun -Confirm:$false} }




      Any idea how I can initiate a full storage migration for the VMs and those pieces on the first LUN?



        • 1. Re: Powercli Storage VMotion
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          The best way is to use the RelocateVM_Task method.

          With that method you can svMotion the VM files to a specific datastores and specify per harddisk to which datastore the VMDK file should go.


          Something like this


          $vmName = <VM-name> 
          = <from-DS-name>
          $targetDS = <target-DS-name>
          = Get-Datastore $targetDS
          = Get-VM $vmName
          = New-Object Vmware.Vim.VirtualMachineRelocateSpec
          $relocationSpec.Datastore = $targetDatastore.ExtensionData.MoRef
          foreach($dev in $vm.ExtensionData.Config.Hardware.Device){      if($dev.Backing.Filename -match  $fromDatastore){           $vmdk = new-object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
                    $vmdk.DataStore = $targetDataStore.MoRef
                    $vmdk.DiskID = $dev.Key
          $relocationSpec.disk += $vmdk
               } } $vm.ExtensionData.RelocateVM($relocationSpec)
          • 2. Re: Powercli Storage VMotion
            nsusa Novice

            Thank you. Found some code to tweak almost the same moment you posted your answer. I think this is based on something you had posted a few months back actually. So, kudos to you.


            The only problem I am having right now is that I cannot get this to play nicely with "-RunAsync". I am on vCenter 4.1, ESX 4.1 U2, PowerCli 5.



            $vms =  Import-Csv "C:\scripts\vms.csv"
            $cloneTask = foreach ($customer in $vms) { Get-HardDisk -vm $customer.name  | Where {$_.Name -eq "Hard disk 1"} | % {Set-HardDisk -HardDisk $_ -Datastore $customer.lun -Confirm:$false } } 
            $ConfigTask = foreach ($vm in $vms){
                  $vmName = Get-VM -Name $vm.name
                  $tgtConfigDS = Get-Datastore -Name $vm.lun
                  $vm = Get-VM -Name $vm.name
                  $hds = Get-HardDisk -VM $vm
                  $spec = New-Object VMware.Vim.VirtualMachineRelocateSpec 
            $spec.datastore = (Get-Datastore -Name $tgtConfigDS).Extensiondata.MoRef
            $hds | %{
                $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
                $disk.diskId = $_.Extensiondata.Key
                $disk.datastore = $_.Extensiondata.Backing.Datastore
                $spec.disk += $disk
            $vm.Extensiondata.RelocateVM_Task($spec, "defaultPriority")


            I have been trying to add it near the -Confirm:$false but that is not working. Any ideas of where I am off track?


            Thank you.

            • 3. Re: Powercli Storage VMotion
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              I'm afraid your mixing 2 different samples here.

              The Set-Harddisk with the Datastore parameter will do a svMotion for 1 VMDK.

              And there is no ASync parameter.


              The call to RelocateVM_Task uses an API method to configure the svMotion.

              The sample I posted earlier will move the VM's config files and will move a selected set of harddisks.

              1 person found this helpful
              • 4. Re: Powercli Storage VMotion
                nsusa Novice

                Understood. Thank you. You have been a great help.

                • 5. Re: Powercli Storage VMotion
                  allencrawford Hot Shot

                  Not to ressurect an old thread, but I came across this when running into more or less the same problem.  Some of the API references seem to have changed since Luc's script was written too, but mainly we needed a bit more capability for our situation (we needed to move templates too, for example).


                  Anyway, in case anyone else is looking for something similar, I posted the script at http://vnugglets.com/2012/12/evacuating-vms-and-templates-from.html.

                  • 6. Re: Powercli Storage VMotion
                    LucD Guru
                    User ModeratorsCommunity WarriorsvExpert

                    Great script, thanks for sharing.