I figured this out with the help of AI.. this works fantastic but not set up for having a VM who's disks are spread across multiple datastores, that would take more work but would most likely use the same coding format as mapping multiple network adapters to different portgroups.
Create CSV with these headers: VMName,SourceNetworkAdapter,DestinationPortgroup,DestinationCluster,DestinationDatastore
# Connect to vCenter
$vCenter = "xxxxxxxxxx"
Connect-VIServer -Server $vCenter
# Import CSV
$csv = Import-Csv "C:\Script\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
#$destDatastoreName = $vmName.DestinationDatastore
# 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
}
}