VMware Cloud Community
tomsmith14
Contributor
Contributor

powercli script to migrate to new cluster and set new vds port groups - multiple nics

hello, im wondering if anyone can help. We have created a new vds in our vcenter and new clusters and i want to migrate vm's from old cluster to a new cluster. The port group names are exactly the same and all exist on the new VDS. The new hosts are only attached to the new VDS... 

If i manually do  vmotion through the gui it works but if i use powershell i have to set the destination port group... I cant seem to figure this out for vm's with multiple nics at the moment... it works if the vm has one Nic

i have attached my sample script if anyone can take a look and offer advice on how to script this as we have too many vm's to manually migrate... 

thanks 

 

Labels (1)
Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

The Portgroup parameter on the Move-VM cmdlet accepts an array of Portgroup objects.
From the parameter help.

"Specifies the destination port groups for the specified virtual machine network adapters. The number of the port groups should be one or equal to the number of the specified network adapters. If one port group and more than one network adapters are specified, you can migrate all network adapters to the specified port group. You cannot use this parameter with the Network parameter."

If only 1 Portgroup is passed, all vNics will be migrated to the same Portgroup


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

Reply
0 Kudos
tomsmith14
Contributor
Contributor

appreciate the response on this ... 

im not the best on powercli still learning... can you explain to me how to write this part of the script for multiple nics  ?

$newpg = get-vdswitch -name "vdsname" | Get-VDPortgroup -name $_.portgroup

currently have

 

 Get-VM -Name $_.machine | Move-VM -Destination $_.destination -PortGroup $newpg -RunAsync -Confirm:$True 

Reply
0 Kudos
LucD
Leadership
Leadership

It depends, should all vNics be migrated to the same Portgroup?
Or should each vNic go to the Portgroup (with the same name) on the destination vDS?


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

Reply
0 Kudos
LucD
Leadership
Leadership

In fact your original script will work if you want all vNics to be migrated to the same destination Portgroup.

If you want to migrate each vNic to the portgroup with the same name on the destination vDS, you could do something like this.
Test first!

####
#Store this script and CSV file under C:\Scripts. The CSV needs two columns, first is labeled machine for the VM name, the second is labeled targethost for target Esxihost, 3rd for portgroup
$csvinput = "c:\temp\intersight\vmmigrationlist.csv"
$vmname = $csv."vmname"
$targetHost = $csv."TargetHost"
#$networkname = $csv."portgroup"
$maxsession = Read-Host "How many concurrent Migrations would you like to run?"
$runsession = 0

echo "---------------Summary----------------"
echo "This script is to perform vMotions of vm's with given max sessions of $maxsession"
echo "--------------------------------------"
Import-Csv $csvinput | foreach {

    echo "------------------------------------------------"
    echo "Checking for active vMotions.... Please wait...."
    echo "------------------------------------------------"

    Do {

        $runsession = (Get-Task | where { $_.name -like "RelocateVM_Task" -and $_.State -eq "Running" }).count

        if ($runsession -ge $maxsession) {
            echo "The current running vMotion sessions is $runsession. Max Concurrent migration sessions : $maxsession. No new vMotion will be started. Next check will be performed in 1 minute."
            Start-Sleep -s 60
            Get-Task | where { $_.State -eq "running" }
        }

        else {
            echo "The current running vMotion sessions is $runsession with a limit of $maxsession. A new vMotion will be started soon."
            Start-Sleep -s 5
        }

    } While ( $runsession -ge $maxsession)

    $destPG = @()
    $vds = Get-VDSwitch -Name "vdsname"
    $vm = Get-VM -Name $_.machine
    Get-NetworkAdapter -VM $vm -PipelineVariable nic | ForEach-Object -Process {
        $destPG += Get-VDPortgroup -VDSwitch $vds -Name $nic.NetworkName
    }

    echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
    echo "The vMotion for will start for below VM ..."
    echo "VM: "$_.machine
    echo "Destination Host: "$_.destination
    echo "VDS Port Group:  $($destPG - join ';')"
    Get-VM -Name $_.machine | Move-VM -Destination $_.destination -PortGroup $destPG -RunAsync -Confirm:$True 
    echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"

}


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

Reply
0 Kudos
tomsmith14
Contributor
Contributor

thanks for your help on this appreciated, 

with regard to your question:

"It depends, should all vNics be migrated to the same Portgroup?
Or should each vNic go to the Portgroup (with the same name) on the destination vDS>?"

its not all the same port groups no, they are all being migarted to the same vds but port groups will differ dpeending on the vm... 

some vm's have 1 nic, some have 6... 

so for e.g. one vm will have 2 network adapters ... 

nic1 = portgroup 775

nic2 = portgroup 885

 

which im hoping i can specify somewhere like a csv and then script a move so it migrates each vm and sets the port group on each nic correctly 

 

Reply
0 Kudos
tomsmith14
Contributor
Contributor

Move-VM : 25/01/2024 14:56:34 Move-VM Different number of network adapters and portgroups specified. Please specify
either a single portgroup to migrate all the network adapters to or the same number of network adapters and portgroups
At C:\temp\intersight\forumscript.ps1:48 char:31
+ ... _.machine | Move-VM -Destination $_.destination -PortGroup $destPG -R ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.Collecti...oupBaseInterop]:List`1) [Move-VM], VimException
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_CheckMoveVmParameters_DifferentNumberNetworkAdaptersAndPortGr
oups,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

Reply
0 Kudos
LucD
Leadership
Leadership

Could it be that on of the vNics is connected to a Portgroup that has no counterpart on the new vDS?


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

Reply
0 Kudos
tomsmith14
Contributor
Contributor

shouldn't be no, new vds is an exact replica in terms of port groups available etc... should be like for like 

Reply
0 Kudos
LucD
Leadership
Leadership

You should check what is in $destPG and how many vNics that VM has


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

Reply
0 Kudos