http://cloudmaniac.net/using-powercli-to-vmotion-vm-between-different-sso-domains-vcenters/
Roman Decker wrote an article about how to move virtual machine between vCenters that are not part of the same SSO domain, using PowerCLI.
I felt like it needed some adjustments to be more dynamic and interactive, so many of the hard coded bits I decided to make the script ask for at runtime.
Credit for the original script goes to Roman Decker.
The script is not perfect, so use at your own risk. I would like to make fault correction for failed login. I just did not have any more time.
Use at your own risk. Good luck.
The Script
$vCenters = "vCenter1", "vCenter2", "vCenter3", "vCenter4"
Disconnect-VIServer -Server * -Force -Confirm:$false
Function SingleSelect() {
param($Options,
[String]$Title
)
$result = $null
while (($result -eq $null) -Or ($result.Count -ne 1)) {
$result = $Options | Out-GridView -Title $Title -PassThru
}
return $result
}
# vCenter Source Details
$srcvCenter = SingleSelect -Options $vCenters -Title "Select Source vCenter"
$SrcCredetials = Get-Credential -Message "Source vCenter Credentials"
$SrcvCenterUserName = $SrcCredetials.UserName
$SrcvCenterPassword = $SrcCredetials.GetNetworkCredential().Password
# vCenter Destination Detail
$DstvCenter = SingleSelect -Options $vCenters -Title "Select Destination vCenter"
$DstCredetials = Get-Credential -Message "Destination vCenter Credentials"
$DstvCenterUserName = $DstCredetials.UserName
$DstvCenterPassword = $DstCredetials.GetNetworkCredential().Password
####################################################################################
# Connect to vCenter Servers
Connect-ViServer -Server $SrcvCenter -User $SrcvCenterUserName -Password $SrcvCenterPassword -WarningAction Ignore | out-null
write-Host -foregroundcolor Yellow "`nConnected to Source vCenter..."
Connect-ViServer -Server $DstvCenter -User $DstvCenterUserName -Password $DstvCenterPassword -WarningAction Ignore | out-null
write-Host -foregroundcolor Yellow "Connected to Destination vCenter..."
# vMotion Details
$vm = SingleSelect -Options (Get-VM -Server $srcvCenter) -Title "Select VM to move"
$DstCluster = SingleSelect -Options (Get-Cluster -Server $DstvCenter) -Title "Select Destination cluster"
$DstHost = Get-VMHost -Location $DstCluster | Select-Object -First 1
$DstDatastore = SingleSelect -Options ($DstHost | Get-Datastore) -Title "Select Destination datastore"
$portgroups = $DstHost | Get-VDSwitch | Get-VDPortgroup
if ($portgroups.count -le 0) {
$portgroups = $DstHost | Get-VirtualPortGroup -Standard
}
$DstPortGroup = SingleSelect -Options ($portgroups) -Title "Select Destination portgroup"
####################################################################################
# Function GetPortGroupObject
function GetPortGroupObject {
Param(
[Parameter(Mandatory=$True)]
[string]$PortGroup
)
if (Get-VDPortGroup -Name $DstPortGroup -ErrorAction SilentlyContinue) {
return Get-VDPortGroup -Name $DstPortGroup
}
else {
if (Get-VirtualPortGroup -Name $DstPortGroup -ErrorAction SilentlyContinue) {
return Get-VirtualPortGroup -Name $DstPortGroup
}
else {
Write-Host "The PorGroup '$DstPortGroup' doesn't exist in the destination vCenter"
exit
}
}
}
function Drawline {
for($i=0; $i -lt (get-host).ui.rawui.buffersize.width; $i++) {write-host -nonewline -foregroundcolor cyan "-"}
}
####################################################################################
# vMotion :)
$networkAdapter = $vm | Get-NetworkAdapter
$vm | Move-VM -Destination $DstHost -NetworkAdapter $networkAdapter -PortGroup $DstPortGroup -Datastore $DstDatastore | out-null
####################################################################################
# Display VM information after vMotion
write-host -foregroundcolor Cyan "`nVM is now running on:"
Drawline
Get-VM $vmToMigrate | Get-NetworkAdapter | Select-Object @{N="VM Name";E={$_.Parent.Name}},@{N="Cluster";E={Get-Cluster -VM $_.Parent}},@{N="ESXi Host";E={Get-VMHost -VM $_.Parent}},@{N="Datastore";E={Get-Datastore -VM $_.Parent}},@{N="Network";E={$_.NetworkName}} | Format-List
####################################################################################
# Disconnect
Disconnect-VIServer -Server * -Force -Confirm:$false
View Comments (2)
Thanks for the updated script!
I was wondering, instead of targeting the destination based on the cluster, could we specify the destination based on specific esxi host ?
The distination host is specified by this line:
$DstHost = Get-VMHost -Location $DstCluster | Select-Object -First 1
You can just alter that to anything you want.