Automation

 View Only
  • 1.  VM Cluster Migration ?

    Posted Mar 01, 2011 02:09 PM

    I have VMs that I need to live migrate from one cluster to another.   Each hosts in both clusters have access to a large datastore we use as a swing volume.  I have created a script that will first migrate the VM to the swing volume, then to a host in the new cluster, and then to its permanent datastore.

    I plan to hand this task off to some other admins to perfom the migrations.  These other admins have no powercli experience, and I have very little.

    As you can see looking at my script that there is no intellegence built in.  I have to rely on the admins to verify the portgroup that the VM is currently connected is avilable on the new hosts, and that the datastore has enough space without completely filling the LUN.  I like to leave 40GB free on the LUN after the migration.

    Does anyone know how I could build in some intellegence that would verify the portgroup exist, and the LUN has enough space? 

    VM_Cluster_Migration.ps1

    ------- Script Start --------

    $tempLun = "SWING_VOLUME_NAME"
    $vmName = (Read-Host " Enter VM Name to Migrate")
    $newHost = (Read-Host " Enter Host to migrate " $vmName " to")
    $newLun = (Read-Host " Enter new Datastore")
    Get-VM $vmName |Move-VM -datastore (Get-datastore $tempLun)
    Get-VM $vmName | Move-VM -Destination (Get-VMHost $newHost) -RunAsync
    Get-VM $vmName |Move-VM -datastore (Get-datastore $newLun)

    -------Script End ---------

    Any help would be greatly appreciated.

    Thanks in advance,

    -geob



  • 2.  RE: VM Cluster Migration ?
    Best Answer

    Posted Mar 01, 2011 04:52 PM

    Try something like this

    $spareMB = 40 * 1KB
    $tempLun = "SWING_VOLUME_NAME" 
    $vmName
    = (Read-Host " Enter VM Name to Migrate") $newHost = (Read-Host " Enter Host to migrate " $vmName " to") $newLun = (Read-Host " Enter new Datastore") $vm = Get-VM -Name $vmName
    $vmDiskSizeMB
    = ($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum / 1KB $pg = Get-VirtualPortGroup -VM $vm | %{$_.Name} $hostPg = Get-VMHost -Name $newHost | Get-VirtualPortGroup | %{$_.Name} $ds = Get-Datastore -Name $newLun
    if
    ($pg | where {$hostPg -notcontains $_}){     Write-Host "Host doesn't have all the required portgroups"} elseif($ds.CapacityMb - $vmDiskSizeMB -lt $spareMB){     Write-Host "Target datastore doesn't have enough free space"} else{     Get-VM $vmName |Move-VM -datastore (Get-datastore $tempLun)     Get-VM $vmName |Move-VM -Destination (Get-VMHost $newHost) -RunAsync
       
    Get-VM $vmName |Move-VM -datastore (Get-datastore $newLun) }

    Note that the datastore size calculation doesn't take into account that a VM's harddisk could be Thin.



  • 3.  RE: VM Cluster Migration ?

    Posted Mar 01, 2011 06:35 PM

    I think thats probably close to what I need, but I keep getting this error.

    Get-VM : A positional parameter cannot be found that accepts argument '$null'.
    At C:\Files\PowerShell\test\Test_VM_Cluster_Migration.ps1:7 char:13
    + $vm = Get-VM <<<<  -Name $vmName $vmDiskSizeMB = ($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum / 1KB
        + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

    Get-VirtualPortGroup : Cannot validate argument on parameter 'VM'. The argument is null or empty. Supply an argument th
    at is not null or empty and then try the command again.
    At C:\Files\PowerShell\test\Test_VM_Cluster_Migration.ps1:8 char:31
    + $pg = Get-VirtualPortGroup -VM <<<<  $vm | %{$_.Name}
        + CategoryInfo          : InvalidData: (:) [Get-VirtualPortGroup], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVi
       rtualPortGroup



  • 4.  RE: VM Cluster Migration ?

    Posted Mar 01, 2011 07:32 PM

    It looks like you had some trouble with copy and paste and that line 7 and line 8 of Luc's script are on the same line on your system.

    Regards, Robert



  • 5.  RE: VM Cluster Migration ?

    Posted Mar 02, 2011 12:30 PM

    After seperated lines 7 and 8, that works great.

    Thanks so much,

    -geob