Automation

 View Only
  • 1.  Powercli Storage VMotion

    Posted Apr 30, 2012 03:48 PM

    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?

    Thanks.



  • 2.  RE: Powercli Storage VMotion

    Posted Apr 30, 2012 04:46 PM

    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> 
    $fromDatastore
    = <from-DS-name>
    $targetDS = <target-DS-name>
    $targetDatastore
    = Get-Datastore $targetDS
    $vm
    = Get-VM $vmName
    $relocationSpec
    = 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)


  • 3.  RE: Powercli Storage VMotion

    Posted Apr 30, 2012 06:33 PM

    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.



  • 4.  RE: Powercli Storage VMotion

    Posted Apr 30, 2012 08:34 PM

    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.



  • 5.  RE: Powercli Storage VMotion

    Posted Apr 30, 2012 09:32 PM

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



  • 6.  RE: Powercli Storage VMotion

    Posted Dec 27, 2012 05:24 AM

    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.



  • 7.  RE: Powercli Storage VMotion

    Posted Dec 27, 2012 07:55 AM

    Great script, thanks for sharing.