Below is a PowerShell example that demonstrates how to move a Virtual Machine (VM) from one host to another using the Move-VM cmdlet. This cmdlet is part of the Hyper-V module, which is used for managing Hyper-V virtualization platform. If you're working with VMware, I've also provided an example at the end using PowerCLI, as the approach differs significantly between Hyper-V and VMware environments.
For Hyper-V Environments:
Prerequisites:
Ensure you have the Hyper-V role installed on both the source and destination hosts.
The Hyper-V module for PowerShell is available by default on systems with the Hyper-V role installed.
You need appropriate permissions to perform VM migrations on both hosts.
PowerShell Code (Hyper-V):
powershell
# Import Hyper-V Module (automatically loaded in PowerShell 3 and later on Hyper-V hosts, but included here for completeness)
Import-Module Hyper-V -ErrorAction Stop
# Define variables
$vmName = "YourVMName" # Name of the VM to move
$sourceHost = "SourceHostName" # Name of the current host of the VM
$destinationHost = "DestinationHostName" # Name of the host to which the VM will be moved
# Get the VM object (assuming it's on the local host where you're running the script; adjust if necessary)
$vm = Get-VM -Name $vmName -ComputerName $sourceHost -ErrorAction Stop
# Check if the VM is running and if live migration is supported
if ($vm.State -eq 'Running') {
Write-Warning "Live migration will be performed. Ensure shared storage is accessible by both hosts."
} else {
Write-Host "The VM is not running. Migration will occur with downtime."
}
# Move the VM to the new host
# The -IncludeStorage parameter moves the VM's storage files to the destination host.
# If your environment uses shared storage (e.g., SAN, SMB), you can omit this parameter.
Move-VM -VM $vm -DestinationHost $destinationHost -IncludeStorage -ErrorAction Stop
Write-Host "VM '$vmName' has been successfully moved from '$sourceHost' to '$destinationHost'."
Important Considerations for Hyper-V Script:
The -IncludeStorage parameter moves the VM's virtual hard disks and configuration files to the destination host. Omit this parameter if you're using shared storage (like SAN or SMB) that's accessible by both hosts.
Ensure both hosts are part of the same domain or have trust configured if they are in different domains.
Live migration (without downtime) requires both hosts to have access to the same shared storage for the VM's virtual hard disks. If shared storage isn't available, use the -IncludeStorage parameter to move the storage as well, which may cause brief downtime.
If you encounter issues connecting to a remote host, verify that PowerShell remoting (WinRM) is enabled on the target host and that there are no firewall restrictions blocking the connection.
Connect to your vCenter Server or the ESXi host directly using Connect-VIServer before running the script.
PowerCLI Code (VMware):
powershell
# Import PowerCLI Module (if not already loaded in your session)
if (-not (Get-Module -Name VMware.PowerCLI -ErrorAction SilentlyContinue)) {
Import-Module -Name VMware.PowerCLI -ErrorAction Stop
}
# Connect to vCenter Server or ESXi Host (if not already connected)
# Example with credentials:
# $cred = Get-Credential
# Connect-VIServer -Server YourVCENTERorESXiHost -Credential $cred
# Define variables
$vmName = "YourVMName"
$destinationHost = "DestinationHostName"
# Get the VM and Destination Host objects
$vm = Get-VM -Name $vmName -ErrorAction Stop
$destHost = Get-VMHost -Name $destinationHost -ErrorAction Stop
# Check if the VM is running
if ($vm.PowerState -eq 'PoweredOn') {
Write-Warning "Live migration (vMotion) will be performed. Ensure shared storage is accessible by both hosts."
} else {
Write-Host "The VM is not running. Migration will occur with downtime."
}
# Move the VM to the new host
# If using shared storage (e.g., NFS, iSCSI), vCenter automatically handles storage accessibility.
Move-VM -VM $vm -Destination $destHost -ErrorAction Stop
Write-Host "VM '$vmName' has been successfully moved to '$destinationHost'."
Important Considerations for PowerCLI Script:
Ensure you have the necessary permissions in vCenter Server or on the ESXi host to perform VM migrations.
This example assumes the storage is shared between hosts. If not, you'll need to specify a datastore accessible by the destination host using the -Datastore parameter with Move-VM.
For live migrations (vMotion), both hosts must have access to the same shared storage. If shared storage isn't available, you'll need to migrate the VM's storage to a compatible datastore on the destination host.
When connecting to vCenter or an ESXi host, ensure your credentials are valid, and check for any network connectivity issues that might prevent authentication.