Powercli script to vMotion VM from one host to another
Here is a PowerCLI script that you can use to migrate a virtual machine from one ESXi host to another using vMotion:
# Import the PowerCLI module
# Connect to vCenter server
Connect-VIServer -Server vcenter.example.com -User firstname.lastname@example.org -Password mypassword
# Define the source and destination ESXi hosts
$sourceHost = Get-VMHost -Name "esxi1.example.com"
$destinationHost = Get-VMHost -Name "esxi2.example.com"
# Define the virtual machine to migrate
$vm = Get-VM -Name "myvm"
# Migrate the virtual machine to the destination host using vMotion
Move-VM -VM $vm -Destination $destinationHost -RunAsync
# Disconnect from vCenter server
Disconnect-VIServer -Server vcenter.example.com -Confirm:$false
This script first imports the PowerCLI module and then connects to the vCenter server. It then defines the source and destination ESXi hosts, as well as the virtual machine to migrate. Finally, it uses the Move-VM cmdlet to migrate the virtual machine to the destination host using vMotion.
Note that vMotion requires shared storage and network connectivity between the source and destination hosts. You should also make sure that the destination host has enough resources (such as CPU and memory) to accommodate the virtual machine.
You can customize this script to suit your specific needs by modifying the parameters such as the vCenter server connection details, the names of the ESXi hosts and virtual machine, and any other options you want to specify.