Hello All,
Working on a script to migrate VMs from a 6.0 vcenter to new 6.7 vcenter in different SSO domains. I'm reading the VM names, destination clusters, and datastores (if provided) from a .CSV file.
If a datastore isn't provided the script goes and find the datastore with most free space and see if it has enough to perform migration at the destination cluster.
I'm getting below error messages. This isn't making sense to me as i pass the name through as $vmToMigrate and it goes out into environment and get the object info and stores it in $vm prior to calling Move-VM cmdlet.
Borrowed main functionality from script posted here:Using PowerCLI to vMotion VM between different SSO domains · cloudmaniac.net
[quote]
Move-VM : 6/26/2019 9:15:33 PM Move-VM One or more objects are specified by name. There is no server list explicitly specified, so an attempt was made to determine a single server by the managed objects passed to the cmdlet as arguments. However the arguments
come from more than one server which makes it impossible to unambiguously select single server.
At C:\Users\***********\Desktop\scripts\Cross VC VMotion\xVmotion.ps1:110 char:10
+ $vm | Move-VM -Destination $destination -NetworkAdapter $networkAd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (System.Collecti...1.VIConnection]:List`1) [Move-VM], ViServerConnectionException
+ FullyQualifiedErrorId : Core_ObnSelector_GetClientListFromCmdletParameters_AmbiguousServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM
Move-VM : 6/26/2019 9:15:34 PM Move-VM Destination container, datastore and portgroups must reside on the same vCenter Server.
At C:\Users\*******\Desktop\scripts\Cross VC VMotion\xVmotion.ps1:110 char:10
+ $vm | Move-VM -Destination $destination -NetworkAdapter $networkAd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Move-VM], VimException
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_CheckMoveVmParameters_DifferentServer,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM
[/quote]
#Import CSV file list.
$vmlist = Import-Csv soggyblock.csv
####################################################################################
# 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..."
####################################################################################
# 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 "-"}
}
#######################################################################################
# Main Script.
foreach ($vmToMigrate in $vmlist)
{
#Define variables to start vmotion.
$DstCluster = $vmToMigrate.'DstCluster'
$DstPortGroup = $vmToMigrate.'DstNetwork-1'
$DstDataStore = $vmToMigrate.'DstDataStore'
#If DstDatastore isn't defined in excel CSV document.find a datastore with enough space.
if(!$DstDataStore)
{
#Get datastore from the destination cluster with most free space.
$Datastore = Get-Cluster $vmToMigrate.DstCluster | Get-Datastore | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1
#Calculate buffer to not fill DS beyond 75%. growth and snapshots.
$DSBuffer = $Datastore.CapacityMB * .75
#Calculate total used space in datastore.
$DSUsedSpaceGB = $Datastore.CapacityMB - $Datastore.FreeSpaceMB
#Calculate if this datastore has enough free space to place VM and not go beyond 75% full.
if($DSUsedSpaceGB + $vmToMigrate.'In Use MB' -gt $DSBuffer)
{
write-host "ERROR: No Datastore found with enough free space. Provision more storage to cluster"
Exit;
}
#If datastore has enough space and doesn't exceed the 75% buffer. Define the Destination Datastore.
else
{ $destinationDatastore =$Datastore.Name }
}
Else
{
$destinationDatastore = $vmToMigrate.DstDataStore
}
####################################################################################
# vMotion :smileyhappy:
$vm = Get-VM $vmToMigrate.VM
$destination = Get-VMHost -Location $DstCluster | Select-Object -First 1
$networkAdapter = Get-NetworkAdapter -VM $vmToMigrate.VM
$destinationPortGroup = GetPortGroupObject -PortGroup $DstPortGroup
$vm | Move-VM -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore | out-null
####################################################################################
# Display VM information after vMotion
write-host -foregroundcolor Cyan "`nVM is now running on:"
Drawline
Get-VM $vm | 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
}