VMware Cloud Community
Zapatone
Enthusiast
Enthusiast

Script in powercli that automate the process of migrating machines from one cpd to other

Hi All,

I'm trying to automate the process of migrating machines from one cpd to other, and when finish migrating the machines, 
detect the empty ESXi of cpd 1 and put them in maintenance mod

I'm getting some lines but don,t work correctly:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Clear-Host

Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Connect-VIServer -server SERVER | Out-Null

cls

$cluster="TEST"

Write-Host We started the contingency for the cluster Cluster of $cluster -BackgroundColor Green -ForegroundColor White

# We started moving all VMs from CPD 1 to CPD 2

Write-Host It starts with the movement of all CPD 1 machines to CPD 2 -BackgroundColor Green -ForegroundColor White

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_2_VM_2" | Set-DrsVMHostRule -Name "TEST_2_VM_2" -Enabled $true

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_2_VM_1" | Set-DrsVMHostRule -Name "TEST_2_VM_1" -Enabled $false

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_1_VM_2" | Set-DrsVMHostRule -Name "TEST_1_VM_2" -Enabled $true

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_1_VM_1" | Set-DrsVMHostRule -Name "TEST_1_VM_1" -Enabled $false

#Once moved everything to CPD 2, we put in maintenance mode the empty ESXi of the cpd 1

sleep 5

#Get Cluster hosts

$vmhosts = Get-Cluster $cluster | Get-VMHost

#Loop through each host

foreach ($vmhost in $vmhosts){

   #Get VM's that are on.

    $poweredonvmcount = ($vmhost | get-vm | where {$_.powerstate -eq 'PoweredOn'}).count

   #If no VM's are on then put host in maintenance mode

    if($poweredonvmcount -eq 0){

        write-host "Putting $vmhost in Maintenance Mode"  

        $updateHostTask = Set-VMHost -VMHost $vmhost -State "Maintenance" -RunAsync

    }

}

------------------------------------------------------------------------------------------------------------------------------------

Can anybody help me?

Thanks in advance!

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership

What does CDP stand for?

I don't think you mean Cisco Discovery Protocol.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Zapatone
Enthusiast
Enthusiast

sorry, i mean Data Processing Ceter (CPD) one CPD in one site and other CPD in other site

Reply
0 Kudos
LucD
Leadership
Leadership

And by activating those DRS rules you will force the vMotions to the other cluster, correct?

What about storage.

Are those VMs sitting on storage that is shared between the 2 clusters?

I also notice you sleep for 5 seconds, are all the vMotions done in 5 seconds?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Zapatone
Enthusiast
Enthusiast

yes, imagine that I have several hosts in the test cluster, some hosts resides in cpd1 and anothers in cpd2, when I change the rules, the machines go from one cpd to other, that can take X minutes depending on the number of machines to migrar, my problem is that I do not know how to do in the script that when you finish migrating the mvs to the hosts of the other cpd, if you have some esxi without running machines, put it in maintenance mode

thanks to try to help!

Reply
0 Kudos
LucD
Leadership
Leadership

You could monitor the ESXi nodes in the cluster that still have VMs running on them.

Once there are no poweredon VMs left, place the ESXi node in maintenance.

Do that in a loop till all ESXi nodes in the cluster are in maintenance mode.

Something like this

$vmhost = Get-Cluster -Name $cluster | Get-VMHost | where{$_.ConnectionState -eq 'Connected'}

while($vmhost.Count -ne 0){

    foreach($esx in $vmhost){

        if((Get-VM -Location $esx | where{$_.PowerState -eq 'PoweredON'}).Count-eq 0){

            Set-VMHost -State Maintenance -Confirm:$false

        }

    }

    sleep 15

    $vmhost = Get-Cluster -Name $cluster | Get-VMHost | where{$_.ConnectionState -eq 'Connected'}

}

There might be a need to disable HA on the cluster before doing this, otherwise the HA might prohibit an ESXi node of going into maintenance mode.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Zapatone
Enthusiast
Enthusiast

Hi!

i try this:

Clear-Host

Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Connect-VIServer -server SERVER | Out-Null

cls

$cluster="TEST"

Write-Host We started the contingency for the cluster Cluster of $cluster -BackgroundColor Green -ForegroundColor White

# We started moving all VMs from CPD 1 to CPD 2

Write-Host It starts with the movement of all CPD 1 machines to CPD 2 -BackgroundColor Green -ForegroundColor White

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_2_VM_2" | Set-DrsVMHostRule -Name "TEST_2_VM_2" -Enabled $true

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_2_VM_1" | Set-DrsVMHostRule -Name "TEST_2_VM_1" -Enabled $false

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_1_VM_2" | Set-DrsVMHostRule -Name "TEST_1_VM_2" -Enabled $true

Get-Cluster -Name $cluster|Get-DRSVMHostRule "TEST_1_VM_1" | Set-DrsVMHostRule -Name "TEST_1_VM_1" -Enabled $false

#Once moved everything to CPD 2, we put in maintenance mode the empty ESXi of the cpd 1

sleep 5

#Get Cluster hosts

$vmhost = Get-Cluster -Name $cluster | Get-VMHost | where{$_.ConnectionState -eq 'Connected'}

while($vmhost.Count -ne 0){

    foreach($esx in $vmhost){

        if((Get-VM -Location $esx | where{$_.PowerState -eq 'PoweredON'}).Count-eq 0){

                 $updateHostTask = Set-VMHost -VMHost $vmhost -State "Maintenance" -RunAsync

        }

    }

    sleep 15

    $vmhost = Get-Cluster -Name $cluster | Get-VMHost | where{$_.ConnectionState -eq 'Connected'}

}

no use your line because it gives me error in powergui--> #Set-VMHost -State Maintenance -Confirm:$false

What this script does is to move the machines well using the rules, and the host that stays empty puts it in maintenance mode, but the other hosts that are in the cluster also try to put them in maintenance mode ... and i dont need this, I just want to keep the vacuum in maintenance

in vCenter i can see this message in the others esxis that try to put in maintenance mode, i cancel the task because i dont want to put those esxi in maintenance mode:

Enter maintenance mode

ejesxiXXXX

The task was

canceled by

a user.

Waiting for all VMs to be powered off or migrated. In a DRS cluster check the Faults page on the DRS tab for troubleshooting.

DOMAIN\USER

SERVER

22/02/2018 12:24:54

22/02/2018 12:24:54

22/02/2018 12:25:41

thanks in advanced!

Reply
0 Kudos
LucD
Leadership
Leadership

Ouch, PowerGui, why the heck would you be using that?

Ok, so not all the ESXi nodes in the cluster need to be placed in maintenance mode.

Which ESXi nodes do need to be placed in maintenance mode?

The ones that have/had VMs running on them?

The ones mentioned in the DRSRules you enabled?

Would be handy to have mentioned those requirements in the original question :smileyconfused:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Zapatone
Enthusiast
Enthusiast

Hi!

• Just know that software to make a script: D

• I need that once the rules are changed, the esxi that remain without machines are put in maintenance mode, the others not because contains the machines have moved there

• Sorry for my poor explanation! I'm a noob!

Reply
0 Kudos
LucD
Leadership
Leadership

So how do I determine which VMs need to be moved?
And indirectly which ESXi nodes need to be placed in maintenance mode?

Or can you provide a list of the ESXi nodes?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Zapatone
Enthusiast
Enthusiast

Imagine a cluster, 4 host, two in each CPD, when we change the rules, the MVs are passed from one CPD to the other, then two hosts should remain empty, the script detects which are empty and puts them in maintenance mode

Reply
0 Kudos
LucD
Leadership
Leadership

I get that, but how does the script know which 2 ESXi nodes have to be placed in maintenance mode?

If all goes well (with the vMotions), ultimately there will be 2 ESXi nodes that are empty, not running any VMs, and those can be placed in maintenance mode.

But what if something goes wrong (a failing vMotion for example), the script will be waiting forever to see 2 empty ESXi nodes.

One option is to "read" the DRS rules you are using, and from there determine which ESXi nodes are being evacuated, and should consequently be placed in maintenance mode.

But since you apparently know in advance which DRS rules to enable, you also know which 2 ESXi nodes you are targeting.

Hence my original question, can you provide, in one form or another, the names of those 2 ESXi nodes to the script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Zapatone
Enthusiast
Enthusiast

can you provide, in one form or another, the names of those 2 ESXi nodes to the script

That's what I do not know, in my infrastructure I have many clusters and many hosts, and I do not know how to determine that when finish migrating machines, the empty hosts put in maintenance mode, and the other hosts with VM,s remain active.

Reply
0 Kudos
LucD
Leadership
Leadership

I give up!


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
EJIE
Contributor
Contributor

No!!

in powercli, how can i detect that when there is no migration task running in my vCenter, the script makes a concrete action!

Reply
0 Kudos