Monday, October 24, 2016

Enter Maintenance Move For ESXi Standard using PowerCLI

I dont know why ESXi Standard Edition had some dummy limitation.
Like when i wana place a server in maintenance mode, I have to manually move all the VM to another server and then it can go to maintenance mode.
its really something silly, as its a very basic feature. anyway
I made a quick script that can migrate all VMHost to another and place the server in maintenance mode.
the script also will do basic check to see if the destination server can handle the load

param(
[parameter(mandatory=$True)]$FromVMHostName,
[parameter(mandatory=$True)]$ToVMHostName
)

try{
    if ($global:DefaultVIServer.count -eq 0){
        write-host "Please Connect to vCenter using Connect-VIServer first" -ForegroundColor Green
       }

    write-host "Performing Server Basic Readiness"
    [int]$FreeRAMOnDestServer=(Get-VMHost -Name $ToVMHostName -ErrorAction Stop).MemoryTotalGB - (Get-VMHost -Name $ToVMHostName -ErrorAction Stop).MemoryUsageGB
    [int]$UsedRAMOnSourceServer=(Get-VMHost -Name $FromVMHostName -ErrorAction Stop).MemoryUsageGB
    if ($FreeRAMOnDestServer -le $UsedRAMOnSourceServer){
    Write-Host "No enough memory on the destination server for migration to complete" -ForegroundColor Red
    Break
    }


    $VmOnHost=Get-VM -ErrorAction Stop| where{$_.host -like $FromVMHostName}
    $I=0
        foreach ($sVM in $VmOnHost){
   
        Write-Progress -Activity "Migrating VM... Please Wait" -Status "Moving $sVM To $ToVMHostName" -PercentComplete ($I/$VmOnHost.Count*100
        Move-VM -VM $sVM.Name -Destination $ToVMHostName
        $I++
        }


    }

    Catch [exception]{
        Write-Host $_.Exception.message
    }


Finally{

if ((Get-VM | where{$_.host -like $FromVMHostName}).count -eq 0){Set-VMHost $FromVMHostName -State Maintenance}

}


If you have any comment or update for this. let me know in the comment
also give a thumb up if you like :)

Post a Comment