1 Reply Latest reply on May 1, 2019 7:46 AM by LucD

    Powercli script to Migrate VMs between two Vcenters

    rxjoseph Novice

      Hi can someone help to finish this script for me please

      I have two Vcenters on vSAN and I want to migrate VMs between the two.

       

      When I do it manually it works so I know the VMotion is working however when I try to script this I get the following error

       

      CSV file

       

      ########SCRIPT to Migrate using a CSV file ####################

       

      #Variables

      $sourceVC = 'SourceVC'

      #Passwords are stored in secure encrypted file

       

      $destVC = 'DestinationVC'

      #Passwords are stored in secure encrypted file

       

       

      # Connect to the vCenter Servers

      $sourceVCConn = Connect-VIServer -Server $sourceVC -Credential (Import-Clixml -Path E:\temp\password\existingVCadminpass.xml)

      $destVCConn = Connect-VIServer -Server $destVC -Credential (Import-Clixml -Path E:\temp\password\newVCadminpass.xml)

       

       

      #Import List of VMs to be migrated (cross VC migration no linked mode required )

      $VMS =Import-Csv -Path E:\temp\password\migratevmlist.csv

       

       

      $vm = Get-VM -Name $vms.name    -Server $sourceVCConn

      $networkAdapter = Get-NetworkAdapter -VM $vm -Server $sourceVCConn

       

      $destination = Get-VMHost -name $VMS.targethost -Server $destVCConn

      $destinationPortGroup = Get-VirtualPortGroup -VirtualSwitch $VMS.targetswitchname -name $VMS.targetportgroup -VMHost $destination

      $destinationDatastore = Get-Datastore -name $vms.targetdatastore -Server $destVCConn

       

      #Migrate VM

      Move-VM -VM $vm -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup -Datastore $destinationDatastore

      ###############################################################################################################################

       

      This is the error I get and it is triggered at the Move-vm statement

      Move-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer' required by parameter 'Destination'. Specified

      method is not supported.

      At line:23 char:30

      + Move-VM -VM $vm -Destination $destination -NetworkAdapter $networkAda ...

      +                              ~~~~~~~~~~~~

          + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBindingException

          + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

       

      Can someone help me please