Move VM Powercli – Migration Issues

This post was originally published on this site



We have deployed a new VMware infrastructure and have prepared a script for performing VM Migration for cross cluster within same vCenter with considering few network parameters and generate output report post completion. Now I have couple of issues with the script and was unable to understand and need to help to resolve them:


1. I want the script to perform the Migration of simultaneous VM’s at same time. The current script run for Individual server, deploy VMTools and then perform the migration tasks for other VM. I have used RunAsync but not good for me.


2. If I am using the } Bracket after deploy VM Tools then the script is not running anything including Move VM and does give any error. It just migrate the VM within same cluster as it does not read Destination cluster Information. Not sure if I am missing any Bracket or I was not able to run it properly.


3. My output Report provides me within Multiple Duplicate entries (13 Times same name to be specific)


Please help me with correcting the details, will really appreciate some input. Below is my Script:


#Author: Amit Singh

#general scope variables, migration targets (Destinationhost, Destinationdatastore, Destinationportgroup)



$batch_input = import-csv ‘CSV FILE LOCATION’ #-Delimiter “;” #| select -ExpandProperty VM_names, PortGroup, PG, Datastore



[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12



Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false



$vCenter = ‘vCENTER NAME’



Connect-VIServer -Server $vCenter -User ‘USERID’ -Password ‘PASSWORD’


$report = @()



foreach ($vmname in $batch_input)





$vm = get-vm -Name $vmname.VM_Names -server $vCenter



#Source Information

$sourceCluster = Get-Cluster $vmname.SourceCluster



$SourceSwitch = Get-VDSwitch -Name $vmname.SourceSW



$SourcePG = $vmname.SourcePG



$pg = Get-VDPortgroup -VDSwitch $vmname.SourceSW -Name $vmname.SourcePG -Server $vCenter



If ($vm.ExtensionData.Network.Count -eq ‘2’) {



$VMAdapter1 = Get-NetworkAdapter -VM $vm | where name -EQ “Network adapter 1” | Select-Object -ExpandProperty ‘NetworkName’



if ($VMAdapter1 -contains $SourcePG)



{ Write-Host “Network Card 1 is a Production Network. Proceed with Migration”}



Else {



Write-Host “Network Card is Not a Production Network. There will be outage”




$VMAdapter2 = Get-NetworkAdapter -VM $vm |where name -EQ “Network adapter 2” | Select-Object -ExpandProperty ‘NetworkName’



If ($VMAdapter2 -eq ‘dvPortGroup_vLAN15_BKP’)



{ Write-Host “Sever 2 NIC is connected to BKP vLAN” -ForegroundColor Cyan



#Disconnect BKP NIC

Write-Host “Reconfiguring backup NIC”


$vm | Get-NetworkAdapter |Where {$_.NetworkName -eq “$VMAdapter2”} | Set-NetworkAdapter -NetworkName $pg -Connected:$false -Confirm:$false




Else {Write-Host “VM has 1 NIC” -ForegroundColor DarkGreen}





#Destination Information


$destSwitch = Get-VDSwitch -Name $vmname.TargetSW


$destinationPG = Get-VDPortgroup -VDSwitch $destSwitch -Name $vmname.TargetPG -Server $vCenter


$destinationDS = Get-Datastore $vmname.TargetDS


$destCluster = Get-Cluster -Name $vmname.TargetCluster


$Desthost = Get-VMHost -Location $destCluster -Server $vCenter | Get-Random


$networkAdapter = Get-NetworkAdapter -VM $vm



Sleep 5


#Initiate VM Movement


Write-Host “Migrating $vm to $Desthost” -ForegroundColor Yellow


Move-VM -VM $VM -Destination $Desthost -NetworkAdapter $networkAdapter -PortGroup $destinationPG -Datastore $destinationDS -Server $vCenter -Confirm:$false -RunAsync


Sleep 10


#Update vmware tools

Write-Host ” Checking and upgrading VMware Tools and VM Compatibility if nesessary” -ForegroundColor Yellow

Get-VM $vm | % { get-view $ } |Where-Object {$_.Guest.ToolsVersionStatus -like “guestToolsNeedUpgrade”} |select name, @{Name=“ToolsVersion”; Expression={$}}, @{ Name=“ToolStatus”; Expression={$_.Guest.ToolsVersionStatus}}| Update-Tools -NoReboot -VM {$_.Name} -Verbose




foreach($vmname in $batch_input){


    $vms = “” | Select-Object VMName, Hostname, IPAddress, Boottime, VMState, TotalNics, ToolsStatus,

          ToolsVersion, HardwareVersion, Portgroup, VMHost



    $vms.VMName = $vm.Name


    $vms.Hostname = $vm.guest.hostname


    $vms.IPAddress = $vm.guest.IPAddress


    $vms.Boottime = $vm.extensiondata.Runtime.BootTime


    $vms.VMState = $vm.extensiondata.Runtime.PowerState


    $vms.TotalNics = $vm.summary.config.numEthernetCards


    $vms.ToolsStatus = $vm.ExtensionData.Guest.ToolsStatus


    $vms.ToolsVersion = $vm.ExtensionData.Guest.ToolsVersion


    $vms.HardwareVersion = $vm.extensiondata.config.Version


    $vms.Portgroup = (get-view $


    $vms.VMHost = (get-view $vm.extensiondata.Runtime.Host).name


    $Report += $vms




write-Host “$vm_name –!!!DONE!!!—“


$report | ConvertTo-Html | out-file ‘OUTPUT FILE LOCATION’

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.