Automation

 View Only
  • 1.  script to shutdown VMs and migrate to different cluster

    Posted Oct 05, 2017 07:11 PM

    Need to shutdown all vms from amd host and migrate to intel. Will the following work?

    $prod_vms = get-content prod_vms.txt

    $dmz_vms = get-content dmz_vms.txt

    $new_prod_cluster = get-cluster prod002

    $new_dmz_cluster = get-cluster dmz002

    foreach ($vm in $prod_vms){

    write-host "shutting down prod-vm $vm"

    $vm | stop-vmguest -confirm:$false

    start-sleep -seconds 60

    move-vm $vm -location $new_prod_cluster

    start-vm $vm -confirm:$false -Runasync

    }

    foreach ($vm2 in $dmz_vms){

    write-host "shutting down  dmz-vm $vm

    $vm2 | stop-vmguest -confirm:$false

    start-sleep -seconds 60

    move-vm $vm2 -location $new_dmz_cluster

    start-vm $vm2 -confirm:$false -Runasync

    }



  • 2.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 05, 2017 07:37 PM

    It should work, but the risky part is the sleep where you rely on the fact that guest OS will have shut down in 60 seconds or less.
    It would perhaps be safer to rely on the VmPoweredOffEvent event.

    You could wait in a While-loop until that event for the specific VM is there (use Get-VIEvent).

    On the Move-VM, if these VMs are not sitting on shared storage visible to source and destination cluster, you might want to add a Datastore option.

    And the Location parameter should be a Destination parameter on the Move-VM.



  • 3.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 05, 2017 08:06 PM

    lucd,

    how do I check if all VMs are powered off before migrating?

    or maybe can this be used instead?

    About Async tasks, the Get-Task cmdlet and a hash table - LucD notes



  • 4.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 05, 2017 08:13 PM

    Running the shutdowns in ASync mode would be faster.
    But you will still need to make sure that a VM is powered off, before doing the Move-VM.



  • 5.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 05, 2017 08:59 PM

    would this work? I have not tested yet.

    $prod_vms = get-content prod_vms.txt

    $dmz_vms = get-content dmz_vms.txt

    $prod_cluster = get-cluster -server vc "PROD"

    $dmz_cluster = get-cluster -server vc "DMZ"

    $tasktab1 = @{}

    $tasktab2 = @{}

    foreach ($vm in $prod_vms) {

    write-host "Shutting down prod-vm $vm"

    $tasktab1[($vm | stop-vmguest -confirm:$false).id] =  $vm

    }

    foreach ($vm2 in $dmz_vms) {

    write-host "shutting down dmz-vm $vm2"

    $tasktab2[( $vm2 | stop-vmguest -Confirm:$false).id] = $vm2

    #$vm2 | stop-vmguest -confirm:$false

    }

    $runningtasks1 = $tasktab1.Count

    $runningtasks2 = $tasktab2.count

    while($runningtasks1 -gt 0){

    get-task | % {

        if($tasktab1.ContainsKey($_.Id) -and $_.State -eq "Success"){

            get-vm $tasktab1[$_.id] | move-vm -Location $prod_cluster -Confirm:$false -RunAsync

            get-vm $tasktab1[$_.id] | Start-VM -Confirm:$false -RunAsync

            $tasktab1.Remove($_.id)

            $runningtasks1--

            }

            elseif($tasktab1.Contains($_.Id) -and $_.state -eq "Error"){

                $tasktab1.Remove($_.id)

                $runningtasks1--

                }

                }

                start-sleep -seconds 15

    while($runningtasks2 -gt 0){

    get-task | % {

    if($tasktab2.ContainsKey($_.Id) -and $_.State -eq "Success"){

            get-vm $tasktab2[$_.id] | move-vm -Location $prod_cluster -Confirm:$false -RunAsync

            get-vm $tasktab2[$_.id] | Start-VM -Confirm:$false -RunAsync

            $tasktab2.Remove($_.id)

            $runningtasks2--

            }

            elseif($tasktab2.Contains($_.Id) -and $_.state -eq "Error"){

                $tasktab2.Remove($_.id)

                $runningtasks2--

                }

                }

                start-sleep -seconds 15



  • 6.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 05, 2017 08:59 PM

    the storage is the same. share across cluster



  • 7.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 06, 2017 05:14 AM

    I'm afraid that will not work, the Stop-VMGuest cmdlet can't be run in the background, it doesn't have a RunAsync switch.

    But you could try something like this (I only show for one cluster to keep the code simple, but it should be trivial to add additional clusters).

    In short:

    • For all VMs in the cluster, we store the VirtualMachine object in a hash table
    • We do the following until the hash table is empty
      • We run through the complete hash table
        • if the VM is powered off
          • We move the VM
          • We remove the VM from the hash table
        • if the VM is not yet powered off
          • We gracefully stop the VM
      • We wait a bit
    • the hash table is empty, all VMs are moved

    $prod_vms = Get-Content prod_vms.txt

    $prod_cluster = Get-Cluster -server vc "PROD"

    # Get all the VM objects in a hash table

    $vmTab = @{}

         Get-VM -Name $prod_vms | %{

        $vmTab.Add($vm.Name,$vm)

    }

    # While there are VM that haven't been moved yet

    while($vmTab.Keys.Count -ne 0){

        $vmTab.GetEnumerator() | %{

            $vmTab[$_.Name] = Get-VM -Name $_.Name

            if($vmTab[$_.Name].PowerState -eq 'PoweredOff'){

                Move-VM -VM $vmTab[$_.Name] -Destination $prod_cluster -Confirm:$false -RunAsync > $null

                $vmTab.Remove($_.Name)

            }

            else{

                write-host "Shutting down prod-vm $($vm.Name)"

                Stop-VMGuest -VM $vmTab[$_.Name] -confirm:$false

            }

        }

        sleep 5

    }



  • 8.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 06, 2017 02:31 PM

    Thanks Luc. I will try this out.



  • 9.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 09, 2017 10:24 PM

    luc

    getting this error when running it

    Exception calling "Add" with "2" argument(s): "Key cannot be null.

    Parameter name: key"

    At line:8 char:5

    +     $vmTab.Add($vm.Name,$vm)

    +     ~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ArgumentNullException

    Exception calling "Add" with "2" argument(s): "Key cannot be null.

    Parameter name: key"

    At line:8 char:5

    +     $vmTab.Add($vm.Name,$vm)

    +     ~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ArgumentNullException



  • 10.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 10, 2017 05:07 AM

    Is there by any chance a blank line in your input file?



  • 11.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 06, 2017 03:17 PM

    Lucd,

    would the following work or do i need to use the VMpoweredOffEvent? Do I need a pause between move-vm and start-VM?

    $prod_vms = get-content prod_vms.txt

    $dmz_vms = get-content dmz_vms.txt

    $new_prod_cluster = get-cluster prod002

    $new_dmz_cluster = get-cluster dmz002

    foreach ($vm in $prod_vms){

    if ($vm.powerstate -eq "PoweredOn") {

    write-host "shutting down prod-vm $vm"

    while ($vm.powerstate -ne "PoweredOff")

    {

    Start-sleep -seconds 5

    }

    move-vm $vm -location $new_prod_cluster -confirm:$false -Runasync

    start-vm $vm -confirm:$false -Runasync

    }



  • 12.  RE: script to shutdown VMs and migrate to different cluster

    Posted Oct 06, 2017 03:57 PM

    That should work, but you will be doing the VMs in sequence, not in parallel