The below script is for reference and it is working as expected but the network setting for the VM is not updating once migration is completed. I'm migrating the VM from Standard Portgroup (Which is connected in the Source vCenter ) to Distributed Portgroup which not updating once the migration is completed.
Source vCenter : 6.0 U3
Target vCenter : 6.7 U2
Migration Type : vCenter to vCenter migration using Move-VM commandlet.
Issue : The network setting for the VM is not updating to DVport from the Target Portgroup and remains the same standard portgroup as in source vcenter.
[string]$VM,
[string]$VMHost
)
If (-not ($VM-and$VMHost))
{
Throw“You must supply a value for-VM and -VMHost”
}
$NLSourceVC="Source vCenter"
$NLTargetVC="Target vCenter"
Write-Host"Enter Credentials"
$Cred01=Get-Credential
try {
$NLSVC=Connect-VIServer-Server $NLSourceVC-Credential $Cred01
$NLTVC=Connect-VIServer-Server $NLTargetVC-Credential $Cred01
}
catch {
Write-Host"Invalid Credential, Please rerun the Script"
Break
}
#####Log output
$VerbosePreference="Continue"
$LogPath=Split-Path $MyInvocation.MyCommand.Path
$LogPathName=Join-Path-Path $LogPath-ChildPath "$VM-$(Get-Date-Format 'HH-mm-dd-MM-yyyy').log"
Start-Transcript$LogPathName-Append -IncludeInvocationHeader
$SrcDC=Get-VM$VM|Get-Datacenter|Select-Object-ExpandProperty Name
$DesDC=Get-Datacenter-VMHost $VMHost|Select-Object-ExpandProperty Name
try {
$DesESXi=Get-VMHost$VMhost-Location $Dest_Datacenter-ErrorAction Stop
}
Catch{
Write-host"Cannot find mentioned Destination ESXi Host $VMhost, Please re-run the script with the correct VMhost"
break
}
$TDVSwitch=Get-VDSwitch-Name 'Target DV Switch Name'-Server $NLTVC
try {
$SourceVM=Get-VM$VM-Location (Get-VM$VM|Get-VMHost) -ErrorAction Stop
}
catch{
Write-host"Cannot find mentioned Source VM $VM, Please re-run the script and make sure the VM is running on the Source vCenter"
break
}
$NetworkAdapter=Get-NetworkAdapter-VM $SourceVM
Write-Host"Getting Source Portgroups"
Write-Verbose"$(Get-Date): Getting Source Portgroups"
$NetworkName=@()
ForEach ($NICin$NetworkAdapter)
{
$NetworkName+=$NIC.Networkname
Write-Host$NetworkName
$SourceVLAN=@()
ForEach ($NWNamein$NetworkName)
{
$VLANInfo=Get-VirtualPortGroup-Name $NWName-VMHost $SourceVM.VMHost
$SourceVLAN+=$VLANInfo.VLanId
$SourceVLANs=$SourceVLAN-join (", ")
Write-Host$SourceVLANs
}
}
Write-Host"Checking for Destination Portgroups, this can take a few minutes..."
Write-Verbose"$(Get-Date): Checking for Destination Portgroups, this can take a Few Minutes..."
$DestinationPortGroup=@()
$FetchPG=$SourceVLANs-split (", ")
Write-Host$FetchPG
try {
ForEach ($FetchPGsin$FetchPG)
{
If ($SrcDC-eq"Kabelweg DC")
{
$TargetPG=Get-VDPortgroup-VDSwitch $TDVSwitch|where {$_.Name-Like"*KBW_VLAN_$FetchPGs" } -ErrorAction Stop
$DestinationPortGroup+=$TargetPG
Write-Host$DestinationPortGroup
$CheckVMs=$TargetPG|Get-VM
}
Else
{
$TargetPG=Get-VDPortgroup-VDSwitch $TDVSwitch|where {$_.Name-Like"*CESSNA_VLAN_$FetchPGs" } -ErrorAction Stop
$DestinationPortGroup+=$TargetPG
Write-Host$DestinationPortGroup
$CheckVMs=$TargetPG|Get-VM
}
}
If ($CheckVMs.Count-lt1)
{
Write-Host"Portgroup $portgroup on the Destination vCenter does not connected to any virtual machines and it is empty. Please test network connectivity first before migrating the VM and re-run script again.
If you performed the check already please continue."
If ((Read-Host"Have you performed Network Connectivity Check on the Empty portgroup? [Yes/No]") -eq"No")
{
break
}
}
}
catch {
Write-Host"Some or All Portgroups on Destination vCenter cannot be found, please make sure they exist and re-run the script again! Exiting..."
Write-Verbose"$(Get-Date): Some or All Portgroups on Destination vCenter cannot be found, Please make sure they exist and re-run the script again! Exiting..."
"Portgroups issue check log file"|Out-File-FilePath $LogPathName-Append
Disconnect-VIServer*-Force
break
}
Write-Host"Getting Source Datastore"
Write-Verbose"$(Get-Date): Getting Source Datastore"
$SourceDS=$SourceVM|Get-Datastore
try {
$DestDatastore=Get-Datastore$SourceDS.Name-Location $DesDC-ErrorAction Stop
}
catch {
Write-Host"Datastore $SourceDS does not exist on the Destination vCenter please check and mitigate"
break
}
Write-host"Destination and Source datastore are a match: $DestDatastore"
Write-Host"(Config check) Source portgroups:"
$NetworkName
Write-Host"(Config check) Source portgroups:"
$SourceVLAN
Write-host"(Config Check) Destination portgroups:"
$DestinationPortGroup| ft -AutoSize
try {
If ((Read-Host"Are Source and Destination Portgroup Names and VLAN Info in the Same order and Matches? [Yes/No]") -eq"Yes" )
{
Write-Host"Initaiting migration for $SourceVM"
$Err=$null
$SourceVM|Move-VM-Destination $DesESXi-NetworkAdapter $NetworkAdapter-PortGroup $DestinationPortGroup-Datastore $DestDatastore-ErrorVariable +Err
}
}
catch {
Write-Verbose"Errors generated $err"
"Migration had some errors, Check Log file at $LogPathName for mitigation actions"|Out-File-FilePath $LogPath-Append
break
}
If ($err.count-eq0)
{
Write-host"Migration completed without errors"
}
Stop-Transcript