VMware Cloud Community
ASHCL
Contributor
Contributor

Move VM Powercli - Migration Issues

Hi,

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"

break;}

$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 $_.id } |Where-Object {$_.Guest.ToolsVersionStatus -like "guestToolsNeedUpgrade"} |select name, @{Name=“ToolsVersion”; Expression={$_.config.tools.toolsversion}}, @{ 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 $vm.extensiondata.network).name

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

    $Report += $vms

}

write-Host "$vm_name --!!!DONE!!!---"

$report | ConvertTo-Html | out-file 'OUTPUT FILE LOCATION'

0 Kudos
9 Replies
LucD
Leadership
Leadership

Care to clarify what you mean by "... I have used RunAsync but not good for me."


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ASHCL
Contributor
Contributor

I have used Move-VM command with -RunAsync command based on understanding that it will move multiple VMs in batch which is not working.

hope I was able to clear my view. Let me know if you need any other clarity. Thanks for addressing

0 Kudos
LucD
Leadership
Leadership

I'm afraid you didn't clarify what exactly doesn't work for you.

With the RunAsync switch the Move-VM will be started as a background task, and your script will continue.

Allowing you to start other background tasks.
Thus having the possibility to have multiple vMotions running in parallel.

The maximum number of such background vMotions might be restricted by your environment.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ASHCL
Contributor
Contributor

I wanted to run multiple vmotions in parallel for my batch of servers and then once motion is done for all servers then vmtools should be updated in one go for all and final report to be obtained for same batch of server.

I am pretty new to this Powercli and need your help

0 Kudos
LucD
Leadership
Leadership

I still don't understand what you mean by "I have used RunAsync but not good for me."

Any solution to run vMotions in parallel would involve the use of the RunAsync switch


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ASHCL
Contributor
Contributor

Then based on the script I have pasted can You guide me if my script can perform vMotion first for all the VMs ( say 10 VMs) in batch file and then perform VM tools upgrade and generate repor. it seems that my script performs vMotion and VM tools upgrade on migrated VM and then move another VM

0 Kudos
LucD
Leadership
Leadership

If you can't be bothered to answer my question concerning your RunAsync remark, I'm afraid I won't be able to spend any more time on this thread.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ASHCL
Contributor
Contributor

withbmy Comment for RunAync not good for me i meant that - I have batch of 10 VMs in CSV but despite using move-vm command with RunAsync paramete I saw only VMs are migrated sequentially one by one and not in parallel.

i have tried on 1G and 10G connection.

i hope this answer your question

0 Kudos
hlot
Contributor
Contributor

if you need two migrate, you can open two powercli, 

if you need three migrate, you can open three powercli, 

of course your need splite vms list to two or three .

0 Kudos