PowerCLI

 View Only

 migration script issues

kwg66's profile image
kwg66 posted Feb 27, 2026 12:49 PM

HI folks - Thanks in advance for your expertise if you could lend a hand.   I have a migration script that I put together using AI and it seems like its really close to working as it should but just can't seem to get over an issue..  

THE ERROR:

"Get-VirtualPortGroup : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Users\xxx\Desktop\Colo_migration_script.ps1:45 char:46
+ ... stPG = Get-VirtualPortGroup -Name $entry.DestinationPortgroup -VMHost ..."

The script attempts to use the csv to define the config at the migration destination which includes mapping a VM's multiple adapters to the appropriate port groups (if many cases the destination portgroup assignment has to change as we move from Nexus fabric to ACI with new VLAN IDs and many VMs have more than i adapter)

So, without further ado, here is my new script that I am struggling with for which the error above was generated:

But first, the variables defined in the csv:

VMName,DestinationCluster,DestinationDatastore,SourceNetworkAdapter,DestinationPortgroup

# Connect to vCenter
$vCenter = "xxxxxxxx"
Connect-VIServer -Server $vCenter


# Import CSV
$csv = Import-Csv "C:\temp\migration_variables.csv"

# Group entries per VM (since multiple rows per VM for multiple NICs)
$vmGroups = $csv | Group-Object VMName

foreach ($vmGroup in $vmGroups) {

    $vmName = $vmGroup.Name
    $vmEntries = $vmGroup.Group

    Write-Host "Processing VM: $vmName" -ForegroundColor Cyan

    # Get VM
    $vm = Get-VM -Name $vmName -ErrorAction Ignore

    # Destination cluster (same for all rows of this VM)
    $destClusterName = $vmEntries[0].DestinationCluster
    $destCluster = Get-Cluster -Name $destClusterName -ErrorAction Ignore

    # Random host selection within cluster
    $destHost = Get-VMHost -Location $destCluster | 
                Where-Object {$_.ConnectionState -eq "Connected"} | 
                Get-Random

    Write-Host "Selected destination host: $($destHost.Name)" -ForegroundColor Yellow

    # Destination datastore
    $destDatastoreName = $vmEntries[0].DestinationDatastore
    $destDatastore = Get-Datastore -Name $destDatastoreName -ErrorAction Ignore

    # Build NIC network mapping
    $networkAdapters = @()

    foreach ($entry in $vmEntries) {

        $nic = Get-NetworkAdapter -VM $vm -Name $entry.SourceNetworkAdapter -ErrorAction Ignore
        $destPG = Get-VirtualPortGroup -Name $entry.DestinationPortgroup -VMHost $destHost -ErrorAction Ignore

        $networkAdapters += @{
            NetworkAdapter = $nic
            PortGroup      = $destPG
        }
    }

    try {
        # Perform migration
        Move-VM -VM $vm `
                -Destination $destHost `
                -Datastore $destDatastore `
                -NetworkAdapter ($networkAdapters.NetworkAdapter) `
                -PortGroup ($networkAdapters.PortGroup) `
                -ErrorAction Ignore

        Write-Host "Migration completed for $vmName" -ForegroundColor Green
    }
    catch {
        Write-Host "Migration failed for $vmName : $_" -ForegroundColor Red
    }
}