Yeah you can do this in the UI but I don’t like clicking so will use a script instead. The following script will migrate virtual machines from one vCentre to another, so long as both have a shared datastore.
$SourceVC = "vcentreA"
$SourceVapp = "vApp"
$DestVC = "vcentreB"
$DestCluster = "Cluster"
$DestFolder = "Folder"
$UserFile = "User.rboadi"
$PassFile = "Pass.rboadi"
$cred = Get-Credential
# Functions ---------------------------------------------------------------------------------
function Log ($text) {
$stamp = (Get-Date).ToString("HH:mm:ss.fff")
Write-Host "$stamp | $text"
}
# Business part of script -------------------------------------------------------------------
Start-Transcript -Path VM-Move-VC.log -Append
# Load password credential from encrypted file
try {
$pass = Get-Content $PassFile -errorAction Stop | ConvertTo-SecureString
$user = Get-Content $UserFile -errorAction Stop
$cred = New-Object System.Management.Automation.PsCredential($user, $pass)
} catch {
Log "ERROR: Failed to load credentials to use"
Log $_
Exit
}
# Disconnect any existing VI Server sessions
if ($DefaultVIServers.Count) {
Log("Disconnect existing vCentre server connections...")
Disconnect-VIServer -Server * -Force -Confirm:$false
}
# Connect to source VC
try {
Log "Connecting to $SourceVC"
$VCconn = Connect-VIServer -Server $SourceVC -Credential $cred -errorAction Stop
} catch {
Log("Unable to connect to vCentre - " + $_)
Exit
}
# Get list of VMs to move
$VMs = Get-VM -Location (Get-vApp $SourceVapp) | Sort
Log "VMXs to reregister..."
$VMs2Move = @()
foreach ($vm in $VMs) {
$vm2move = "" | Select Name, Path
$vm2move.Name = $vm.name
$vm2move.Path = $vm.ExtensionData.Config.Files.VmPathName
$VMs2Move += $vm2move
Log ($vm2move.Name + " " + $vm2move.Path)
}
#$VMs | Get-View | %{$_.Config.Files.VmPathName} | Sort
# Unregister VMs
foreach ($vm in $VMs) {
Log ("Unregister " + $vm.Name)
Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false
}
Disconnect-VIServer -Server $VCconn -Confirm:$false
# Connect to destination VC
try {
Log "Connecting to $DestVC"
$VCconn = Connect-VIServer -Server $DestVC -Credential $cred -errorAction Stop
} catch {
Log("Unable to connect to vCentre - " + $_)
Exit
}
# Register VMs
foreach ($vm in $VMs2Move) {
Log ("Register " + $vm.Name)
New-VM -VMFilePath $vm.Path -VMHost (Get-Cluster $DestCluster | Get-VMHost | Get-Random) -Location (Get-Folder $DestFolder)
}
Disconnect-VIServer -Server $VCconn -Confirm:$false
Stop-Transcript