PowerCLI

 View Only
Expand all | Collapse all

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

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

    Posted Jan 25, 2024 01:37 PM

    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 

     



  • 2.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 01:47 PM

    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



  • 3.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 01:50 PM

    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 



  • 4.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 02:00 PM

    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?



  • 5.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 02:32 PM

    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 ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
    
    }


  • 6.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 02:50 PM

    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 

     



  • 7.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 02:55 PM

    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



  • 8.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 03:14 PM

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



  • 9.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 03:15 PM

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



  • 10.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Jan 25, 2024 03:17 PM

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



  • 11.  RE: powercli script to migrate to new cluster and set new vds port groups - multiple nics

    Posted Dec 11, 2025 10:37 PM

    Hi LucD - running into the same error about the # of adapters not matching the # of portgroups.   I have 2 adapters in the VM and simply want both to map to the same destination portgroup.   Based on the docs this should work but it does not, unless I'm misinterpreting.   

    My code example I'm using is literally the example provided on the move-vm info page: 

    https://developer.broadcom.com/powercli/latest/vmware.vimautomation.core/commands/move-vm

    $vm = Get-VM 'myVM' -Location 'myVMhostOnVC1'
    $destination = Get-VMHost 'MyVMhostOnVc2'
    $networkAdapter = Get-NetworkAdapter -VM $vm
    $destinationPortGroup = Get-VDPortgroup -VDSwitch 'myVDSwitchOnVC2' -Name 'myPortGroup'
    $destinationDatastore = Get-Datastore 'MyDatastoreOnVc2'
    $destinationStoragePolicy = Get-SpbmStoragePolicy 'MyStoragePolicyOnVc2'
    $vm | Move-VM -Destination $destination -NetworkAdapter $networkAdapter -PortGroup $destinationPortGroup 

    -------------------------------------------