If for some reason vmotion is not available in the environment and a large number of vms need to be migrated from one host to another (specially when they are of different versions), then the following script can be used. It will check the power state of the vm, power it off if needed and then migrate it. I have a separate script to power on vms.  That can also be included at the bottom of this one. They both read of the same list.

 

  • First a CSV file (poweredonvms.csv) containing list of servers to be migrate.
name
vmserver01
vmserver02
vmserver03
vmserver04
vmserver05
vmserver06
vmserver07
vmserver08
vmserver09
vmserver10

 

  • And here is the actual Windows Powershell script. To run from  vSphere PowerCLI, remove "Add-PSSnapin VMware.VimAutomation.Core" at the beginning of the code. After shutting off the vms and migrating them, it sends out an email with the list of vms.
  • It first checks if a vm is powered off, if yes it gets migrated. If not it checks the status of VMware Tools, depending upong which the vm can be gracefully shutdown or forced to shutdown. The list will show which method was used.
Add-PSSnapin VMware.VimAutomation.Core

$vcenter="vcenter.domain.local"

#Connect to vcenter server
connect-viserver $vcenter

#Import vm name and ip from csv file
Import-Csv poweredonvms.csv |
foreach {
    $strNewVMName = $_.name
  
    #Generate a view for each vm to determine power state
    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $strNewVMName}
  
           if ($vm.Runtime.PowerState -eq "PoweredOff") {
               if ($vm.config.Tools.ToolsVersion -ne 0) {
                   Write-Host "Powered off. Moving VM ++++++ $strNewVMName ----"
                   Move-VM -Destination <destination host> -VM $strNewVMName -Confirm:$false
                 
                   #For generating email
                   $Report += $strNewVMName + " --- VM powered off and moved. `r`n"
               }
           } else {
            if ($vm.config.Tools.ToolsVersion -ne 0) {
                   Write-Host "VMware tools installed. Graceful OS shutdown ++++++++ $strNewVMName ----"
                   Shutdown-VMGuest $strNewVMName -Confirm:$false
                Sleep 120
                   Move-VM -Destination <destination host> -VM $strNewVMName -Confirm:$false
                   #For generating email
                   $Report += $strNewVMName + " --- VMware tools installed. Graceful OS shutdown and vm moved. `r`n"
               }
               else {
                   Write-Host "VMware tools not installed. Force VM shutdown ++++++++ $strNewVMName ----"
                Stop-VM $strNewVMName -Confirm:$false
                Sleep 60
                Move-VM -Destination <destination host> -VM $strNewVMName -Confirm:$false             
                #For generating email
                   $Report += $strNewVMName + " --- VMware tools not installed. Force VM shutdown and vm moved. `r`n"
              
               }
         }
}

write-host "Sleeping ..."
Sleep 30

#Send out an email with the names
$emailFrom = "noreply@domain.com"
$emailTo = "noreply@domain.com"
$subject = "List of vms migrated"
$smtpServer = "smtp server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $Report)

#Disconnect to vcenter server
disconnect-viserver $vcenter -Confirm:$false