VMware Cloud Community
vXav
Expert
Expert

Evacuate a host without DRS

Hey guys,

I just wanted to share, with my fellow poor admins running vSphere standard, a script I put together to safely evacuate a host from all its VMs without DRS.

Script bellow attached in ps1. More info there.

Function Evacuate-VMHost {

param (
   [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyname=$True)]
   [VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]
   $VMHost,

   [ValidateRange(1,100)]
   [int]
   $VMHostMaxCPUUsagePercent = 75,

   [ValidateRange(1,100)]
   [int]
   $VMHostMaxMEMUsagePercent = 75,

   [int]
   $VMHostMaxVCpuPerCore = 9,

   [VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost[]]
   $ExcludedVMHost,

   [VMware.VimAutomation.ViCore.types.V1.Inventory.VirtualMachine[]]
   $ExcludedVM,

   [switch]
   $fullyAutomated,

   [switch]
   $Whatif
)

Try {

   IF ($VMHost.connectionstate -eq "connected") {

   $VM = $VMHost | Get-VM | where {$_ -notin $ExcludedVM}

   $VM | where powerstate -eq poweredon | ForEach-Object {
  
   $CurVM = $_

   $PossibleHost = Get-VMHost `
  | Where name -ne $VMHost.name `
  | where {$_ -notin $ExcludedVMHost} `
  | where connectionstate -eq "connected" `
  | where {(Compare-Object $CurVM.ExtensionData.network.value $_.ExtensionData.network.value).sideindicator -notcontains "<="}

   $i = 0
   $choice = "a"

   $selectedVMHost = $PossibleHost | ForEach-Object {
  
   $i++

   $HostVM = $_ | get-vm | where powerstate -eq poweredon

   [pscustomobject]@{
  id = $i
  name = $_.name
   "ProjectedCpuUsage" = [math]::round(($_.CpuUsageMhz + $CurVM.ExtensionData.Runtime.MaxCpuUsage) / $_.CpuTotalMhz * 100,1)
   "ProjectedMemUsage" = [math]::round(($_.MemoryUsageMB + $CurVM.memoryMB) / $_.MemoryTotalMB * 100,1)
   "ProjectedVCPUperCORE" =[math]::round(($HostVM | Measure-Object -Property numcpu -Sum).sum / $_.NumCpu,1)
   "Projected#LiveVM" = $HostVM.count + 1
   }

   } | where {$_.ProjectedCpuUsage -lt $VMHostMaxCPUUsagePercent -and $_.ProjectedMemUsage -lt $VMHostMaxMEMUsagePercent -and $_.ProjectedVCPUperCORE -lt $VMHostMaxVCpuPerCore}

   IF ($selectedVMHost) {

   $BestVMHost = $selectedVMHost | where id -eq ($selectedVMHost | select id,@{l="sum";e={$_.ProjectedCpuUsage + $_.ProjectedMemUsage}} | Sort-Object sum | select -First 1).id

   ($selectedVMHost | where id -eq $BestVMHost.id).id = "*"

   IF (!$fullyAutomated) {

   Clear-Host

   $_ | select name,powerstate,numcpu,memorygb
  
   $selectedVMHost | Sort-Object id | ft -au

   Write-Host "Select host manually by its ID"
   Write-Host "Press enter to follow the recommendation ( * )"
   Write-Host "Enter N to skip this VM"

   While ($choice -notin @("","n") -and $choice -notin (1..$i)) { $choice = Read-Host " " }

   IF (!$Choice) {$selectedVMHost = $BestVMHost}
   ELSEIF ($choice -eq "n") {Write-Warning "$($CurVM.name) skipped"}
   ELSE {$selectedVMHost = $selectedVMHost | where id -eq $Choice}

   } ELSE {
   $selectedVMHost = $BestVMHost
   }

   IF ($choice -ne "n") {

   Write-Host "$($CurVM.name) moving to $($selectedVMHost.name)" -ForegroundColor green

   $params = @{VM = $_ ; Destination = get-vmhost $selectedVMHost.name}

   IF ($Whatif) {$params.Add('whatif', $true)}

   Move-VM @params | Out-Null

   }

   } ELSE {Write-Warning "There is no host capable of fulfilling the destination resource requirements"}

   }

   } ELSE {Write-warning "$($VMHost.name) is in a $($VMHost.connectionstate) state"}

} CATCH {
   Write-Error $_.Exception -ErrorAction stop
}

Reply
0 Kudos
8 Replies
jakerpowercli
VMware Employee
VMware Employee

Thank you for sharing! You can link your github code to the samples on code.vmware.com too!

All Samples - Samples - VMware {code}

Cheers,

Jake

Reply
0 Kudos
vmrulz
Hot Shot
Hot Shot

Thanks for posting I'll give it a shot. We have DRS but we have this new conundrum call Nvidia GRID vGPU which only recently VMware supported manual vmotion capabilities. DRS and vGPU however is not on any road map I've seen. You don't realize how nice DRS is until you don't have it.

I wonder how difficult it would be to add check for available GPU slot on a destination host?

Ron

Reply
0 Kudos
vmrulz
Hot Shot
Hot Shot

Call me stupid but what is the syntax to run this script?

pastedImage_1.png

Reply
0 Kudos
LucD
Leadership
Leadership

What did you copy into that .ps1 file?
The complete function from the attachment?

If yes, then the .ps1 file only contains the definition of the function.

To make a function in a .ps1 file known to your PS session, you have to dot-source the .ps1 file.

PS> . .\EvacNoDRS.ps1

Note the 2 dots with a space between them.

Now the function is "known" to your PS session, and you can call it

PS> Evacuate-VMHost -VMHost YourEsxiNode


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

Reply
0 Kudos
vmrulz
Hot Shot
Hot Shot

LucD

Thanks for the fast reply. I errantly copied inline text from this thread rather than copying the zip file. It was full of extra spaces thus the poor output. I'll give it a shot using the proper script and syntax

Reply
0 Kudos
vmrulz
Hot Shot
Hot Shot

I was able to get it working as you mentioned. I do not know what syntax I need to pass hostnames I don't want it to attempt to use. It would be easier if we could just pass it a clustername so it wouldn't attempt to migrate to hosts outside of a given cluster like it does now and fails due to no shared storage.

Evacuate-VMHost -VMHost (get-vmhost FQDN) -fullyautomated -excludedvmhost ??

Ron

Reply
0 Kudos
LucD
Leadership
Leadership

That would be something like this (excluding the ESXi nodes FQDN2 and FQDN3)

Evacuate-VMHost -VMHost (Get-VMHost -Name FQDN1) -FullyAutomated -ExcludedVMHost (Get-VMHost -Name FQDN2,FQDN3)


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

Reply
0 Kudos
vmrulz
Hot Shot
Hot Shot

lucD Thanks again for being so connected to this community. The syntax works great albeit a lot of exclusions for me. I'll have to see if I can cobble together a cluster variable to cut down on exclusions.

Lest I forget, thanks also to the original script creator you guys are a great help to those of us not inclined to spend the time learning and writing code.. I'm trying but we seem to be headed down terraform, ansible and python paths.

Ron

Reply
0 Kudos