locked
Use cmdlet to live migrate VM changing the vNetwork. RRS feed

  • Question

  • Good morning.

    I have to live migrate VMs from “Hyper-v on Server 2012” to “Hyper-v Server 2012 R2”.
    Well, at the same time I have to migrate all the storage (Config, Snapshots, Pagingfiles and VHDs).
    Up to here everything is ok. 
    At the same time without loose the VMs connectivity I have to change the vNetwork because the vNetworks on the new Hosts are different.

    I will use Move-VM powershell cmdlet, example:

    Move-VM $VM remoteServer –VirtualMachinePath $Path1 –SnapshotFilePath $Path1 –SmartPagingFilePath $Path1 –IncludeStorage –VHDs @(@{"SourceFilePath" = $OldVHDPath1; "DestinationFilePath" = $NewVHDPath1}, @{"SourceFilePath" = $OldVHDPath2; "DestinationFilePath" = $NewVHDPath2}) 

    When I do it manually through the Hyper-v Console, it ask me to change it:


    How can I change vSwitch at the same time that I Move the VM using a cmdlet without loosing connection?

    Thanks in advance and have a nice week,
    Angel Biurrun.

     
    Thursday, October 15, 2015 10:13 AM

Answers

All replies

  • is renaming the destination vswitch to match the source vswitch out of the question? doesn't seem like you can do it any other way if you need the VM to stay up
    Thursday, October 15, 2015 2:04 PM
  • Thanks Armin19, but I need to change the vSwitch on VM. If Hyper-v can do it trhough the console, sure that I can do it through a Powershell Script at the same time. 

    I just need the right cmdlet and how to use with move-vm cmdlet.

    Regards,

    Angel Biurrun.

    Thursday, October 15, 2015 4:18 PM
  • yes but if the vSwitches both had the same name you would not need to do anything, it would just migrate over

    move-vm doesn't seem to be able to accomodate repointing the vswitch but let's see if anyone else has any ideas here...

    Thursday, October 15, 2015 4:30 PM
  • I know Armin19. 

    But for company policies the other cluster has a diferent vSwitch name.

    Regards,

    Angel Biurrun.

    Thursday, October 15, 2015 5:12 PM
  • This is the article that you were searching for: http://blogs.msdn.com/b/virtual_pc_guy/archive/2014/02/12/using-powershell-to-live-migrate-to-a-computer-with-a-different-virtual-switch.aspx.

    The short answer is to generate a compatibility report using Compare-VM, adjust the output to indicate which target virtual switch to connect it to and then submit the results to Move-VM.


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent blog contributor, not an Altaro employee. I am solely responsible for the content of my posts.

    • Proposed as answer by Leo Han Monday, November 2, 2015 2:47 AM
    • Marked as answer by Leo Han Wednesday, November 4, 2015 7:14 AM
    Thursday, October 15, 2015 5:30 PM
  • Many Thanks for your Link Eric, but, as Ben said in the last line in the link: "After doing this I just need to fix the incompatibilities and pass the results into Move-VM."

    Have a Nice weekend.

    Angel Biurrun C.

    Friday, October 16, 2015 1:20 PM
  • What is the "but"? There is no "but". This is how you Live Migrate a VM while changing the connected virtual switch. It's unfortunate that he chose to use a screenshot rather than make the script available for copy/paste, but it clearly shows that the fix is made prior to the move and that the virtual machine is Live Migrated without error.

    Eric Siron
    Altaro Hyper-V Blog
    I am an independent blog contributor, not an Altaro employee. I am solely responsible for the content of my posts.

    Friday, October 16, 2015 1:28 PM
  • Hello Eric.

    I will adapt his cmdlets in my PS script, on Tuesday I will test it and let you know the results.

    I really understand that he can see the incompatibilities but he has to resolve how to applied incompatibility at the same time with move-vm.

    Best Regards,

    Angel Biurrun C.

    Friday, October 16, 2015 1:54 PM
  • Hello.

    I’m testing this command to check the incompatibilities and I’m getting the error below.

    $VMMigTest = Compare-VM `

      -ComputerName TestHost1.domain.com `

      -Name Test1 `

      -DestinationHost TestHost2 `

      VirtualMachinePath C:\ClusterStorage\Volume1\Test1\ `

      –SnapshotFilePath C:\ClusterStorage\Volume1\Test1\ `

      –SmartPagingFilePath C:\ClusterStorage\Volume1\Test1\ `

      –IncludeStorage `

      –VHDs (@{"SourceFilePath"="C:\ClusterStorage\CSV1\Test1\Test1_disk_1.vhdx";"DestinationFilePath"="C:\ClusterStorage\Volume1\Test1\Test1_disk_1.vhdx"},@{"SourceFilePath"="C:\ClusterStorage\CSV1\Test1\Test1_disk_2.vhdx";"DestinationFilePath"="C:\ClusterStorage\Volume2\Test1\Test1_disk_2.vhdx"})

     

     

    Compare-VM : A parameter is invalid. Cannot specify both a common destination path and individual

    destination path for the migration operation.

    At C:\Users\angelbc\Documents\Scripts\test Vm_Compare.ps1:1 char:14

    + $VMMigTest = Compare-VM `

    +              ~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (:) [Compare-VM], VirtualizationInvalidArgumentException

        + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.CompareVMCommand

     

     

    What I’m doing bad? 

    Have a nice weekend,

    Angel Biurrun C.

    Wednesday, October 21, 2015 11:59 AM
  • start simple, run this from TestHost1:

    Compare-VM –Name Test1 –DestinationHost TestHost2

    then try:
    Compare-VM –Name Test1 –DestinationHost TestHost2 -includestorage

    • Proposed as answer by Leo Han Monday, November 2, 2015 2:48 AM
    • Marked as answer by Leo Han Wednesday, November 4, 2015 7:14 AM
    Wednesday, October 21, 2015 12:22 PM
  • Thanks for your answer armin19.

    I did it but I need to move each disk to a different destination (Company policies).

    I get the error when I use the “-Vhds<System.Collections.Hashtable[]>”.

    Regards,

    Angel Biurrun C.


    • Edited by abiurrunc Wednesday, October 21, 2015 12:42 PM
    Wednesday, October 21, 2015 12:42 PM
  • When you use a compatibility report with Move-VM, you cannot specify any parameters other than the compatibility report. See the help for Move-VM and look at the second parameter set.

    You need to make all path changes while gathering the report. Look at the third parameter set for Compare-VM and you'll see that it's much like the set that you've been trying to use for Move-VM.

     Compare-VM [-VM] <VirtualMachine> [-DestinationHost] <String> [-AsJob] [-DestinationStoragePath <String>] [-IncludeStorage] [-ResourcePoolName <String>] [-RetainVhdCopiesOnSource] [-SmartPagingFilePath <String>] [-SnapshotFilePath <String>] [-Vhds <Hashtable[]>] [-VirtualMachinePath <String>] [-Confirm] [-WhatIf]
     [<CommonParameters>]
    Populate all of the fields that you can in Compare-VM to create your $VMMigTest object, then use Ben's template to change its virtual switch, then pass it to Move-VM as a single parameter.


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent blog contributor, not an Altaro employee. I am solely responsible for the content of my posts.


    Wednesday, October 21, 2015 1:26 PM
  • Thanks for your answer armin19.

    I did it but I need to move each disk to a different destination (Company policies).

    I get the error when I use the “-Vhds<System.Collections.Hashtable[]>”.

    Regards,

    Angel Biurrun C.


    more than a year later i ran into the issue you described.  remove -IncludeStorage option, it's redundant if you're using -VHDs option
    Monday, December 12, 2016 8:07 PM
  • I'm trying to use this process, but getting an error when trying to change the nic in the compatibility report.  

    I started a new thread about it, if Eric or anyone else has time to take a look and perhaps sees where I'm going wrong, I'd appreciate a response.  

    Thanks!

    Monday, November 20, 2017 2:10 PM
  • For Connect-VMNetworkAdapter:

    1. the name for the destination switch has to be on the local host or it fails - you can not just put the name of the destination VSwitch in - it has to actually reside at source host or the command fails.
    2. If you create a 'dummy' local Vswitch that is not connected to network on source host - you will lose network on VM until the migration completes. (but seems to be the only way that Connect-VMNetworkAdapter will not fail.
    3. The GUI actually makes the change at time of migration - only 1 ping is lost.  I have NOT found a powershell way to do this.

    I found that even after "adjusting the NIC" as shown in other scripts (to a new local NIC that has same name as remote server - which drops the network btw) - If I just passed the same compatibility report to the Move-VM command it would fail - as it is still 'incompatible' (changing the NIC does not update the report) - after changing the NIC I then had to refresh the compatibility report - then the migration would succeed.

    Not working:

        $VMReport.Incompatibilities | ?{$_.MessageID -eq 33012} | %{Connect-VMNetworkAdapter $_.source -SwitchName "vSwitch1"} 
        Move-VM -CompatibilityReport $VMReport -AsJob

    Error

    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
    --     ----            -------------   -----         -----------     --------             -------                  
    10     Job10                           NotStarted    True                                     Move-VM -Compatibi...
    
    
    
    PS C:\Windows\system32> Get-Job 10 | Receive-Job -Keep
    Virtual machine migration operation failed at migration destination.
    The virtual machine 'PACEOM01' is not compatible with physical computer '3215R05-19'.
    Virtual machine migration operation for 'PACEOM01' failed at migration destination '3215R05-19.MSCPEC.COM'. (Virtual machine ID E2EC2CAF-31D0-407D-860A-EC0252C1842F)
    The virtual machine 'PACEOM01' is not compatible with physical computer '3215R05-19'. (Virtual machine ID E2EC2CAF-31D0-407D-860A-EC0252C1842F)
    Could not find Ethernet switch 'MGMT'.
    Could not find Ethernet switch 'MGMT2'.
        + CategoryInfo          : NotSpecified: (:) [], VirtualizationException
        + FullyQualifiedErrorId : Unspecified
    

    Step by step break down of error.

    1. Run the report and see the issue with switches.

    PS C:\> (Compare-VM -Name PACEOM01 -DestinationHost 3215R05-19 -IncludeStorage -DestinationStoragePath e:\Hyper-V\PACEOM01).Incompatibilities |fl *
    
    
    Message   : Virtual machine migration operation for 'PACEOM01' failed at migration destination '3215R05-19.MSCPEC.COM'. (Virtual machine ID 
                E2EC2CAF-31D0-407D-860A-EC0252C1842F)
    MessageId : 21026
    Source    : 
    
    Message   : The virtual machine 'PACEOM01' is not compatible with physical computer '3215R05-19'. (Virtual machine ID E2EC2CAF-31D0-407D-860A-EC0252C1842F)
    MessageId : 24000
    Source    : 
    
    Message   : Could not find Ethernet switch 'MGMT'.
    MessageId : 33012
    Source    : VMNetworkAdapter (Name = 'Network Adapter', VMName = 'PACEOM01') [VMId = 'e2ec2caf-31d0-407d-860a-ec0252c1842f']
    
    Message   : Could not find Ethernet switch 'MGMT2'.
    MessageId : 33012
    Source    : VMNetworkAdapter (Name = 'Network Adapter', VMName = 'PACEOM01') [VMId = 'e2ec2caf-31d0-407d-860a-ec0252c1842f']

    So I need to attach these to vSwitch1 on the destination host.

    Here are my existing source switches:

    Name  SwitchType NetAdapterInterfaceDescription                            
    ----  ---------- ------------------------------                            
    MGMT  External   QLogic BCM57800 10 Gigabit Ethernet (NDIS VBD Client) #131
    MGMT2 External   QLogic BCM57800 10 Gigabit Ethernet (NDIS VBD Client) #130
    Corp  External   QLogic BCM57800 Gigabit Ethernet (NDIS VBD Client) #133   
    

    So if I now try to run the commands that everyone point at it fails - not able to find a vSwitch with that name "vSwitch1".

    PS C:\> $VMReport = Compare-VM -Name PACEOM01 -DestinationHost 3215R05-19 -IncludeStorage -DestinationStoragePath e:\Hyper-V\PACEOM01
    
    PS C:\> $VMReport.Incompatibilities 
    
    Message                                                                                                                                                              
    -------                                                                                                                                                              
    Virtual machine migration operation for 'PACEOM01' failed at migration destination '3215R05-19.MSCPEC.COM'. (Virtual machine ID E2EC2CAF-31D0-407D-860A-EC0252C1842F)
    The virtual machine 'PACEOM01' is not compatible with physical computer '3215R05-19'. (Virtual machine ID E2EC2CAF-31D0-407D-860A-EC0252C1842F)                      
    Could not find Ethernet switch 'MGMT'.                                                                                                                               
    Could not find Ethernet switch 'MGMT2'.                                                                                                                              
    
    
    
    PS C:\> $VMReport.Incompatibilities | ?{$_.MessageID -eq 33012}
    
    Message                                 MessageId Source                                                                                                          
    -------                                 --------- ------                                                                                                          
    Could not find Ethernet switch 'MGMT'.      33012 VMNetworkAdapter (Name = 'Network Adapter', VMName = 'PACEOM01') [VMId = 'e2ec2caf-31d0-407d-860a-ec0252c1842f']
    Could not find Ethernet switch 'MGMT2'.     33012 VMNetworkAdapter (Name = 'Network Adapter', VMName = 'PACEOM01') [VMId = 'e2ec2caf-31d0-407d-860a-ec0252c1842f']
    
    
    
    PS C:\> $VMReport.Incompatibilities | ?{$_.MessageID -eq 33012} | %{Connect-VMNetworkAdapter $_.source -SwitchName "vSwitch1"} 
    Connect-VMNetworkAdapter : Hyper-V was unable to find a virtual switch with name "vSwitch1".
    At line:1 char:61
    + ...  33012} | %{Connect-VMNetworkAdapter $_.source -SwitchName "vSwitch1" ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Connect-VMNetworkAdapter], VirtualizationException
        + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.ConnectVMNetworkAdapter
     
    Connect-VMNetworkAdapter : Hyper-V was unable to find a virtual switch with name "vSwitch1".
    At line:1 char:61
    + ...  33012} | %{Connect-VMNetworkAdapter $_.source -SwitchName "vSwitch1" ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Connect-VMNetworkAdapter], VirtualizationException
        + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.ConnectVMNetworkAdapter
     


    This is my workaround (with VM network outage), after creating a dummy local switch & then running the report twice - once to find the issue - fix it & a second time to create a clean report so the move will complete.

    $targethost = "3215R05-19" #Read-Host "Name of target Hyper-V Host" 
    $VMS = Get-VM | Out-Gridview -Title "Running VM’s" -Passthru
    
    ForEach ($VM in $VMS) 
        { 
        md \\$targethost\e$\Hyper-V\$($VM.Name) -ErrorAction SilentlyContinue
        $VMReport = Compare-VM -Name $VM.Name -DestinationHost $targethost -IncludeStorage -DestinationStoragePath e:\Hyper-V\$($VM.Name) 
        ($VMReport.Incompatibilities).MessageID
        if ($VMReport.Incompatibilities | ?{$_.MessageID -eq 33012}) {
        $VMReport.Incompatibilities | ?{$_.MessageID -eq 33012} | %{Connect-VMNetworkAdapter $_.source -SwitchName "vSwitch1"} 
        $VMReport = Compare-VM -Name $VM.Name -DestinationHost $targethost -IncludeStorage -DestinationStoragePath e:\Hyper-V\$($VM.Name)
        }
        Move-VM -CompatibilityReport $VMReport -AsJob
        }
    
    21026
    24000
    33012
    33012
    
    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
    --     ----            -------------   -----         -----------     --------             -------                  
    11     Job11                           NotStarted    True                                     Move-VM -Compatibi...
    21026
    24000
    33012
    33012
    12     Job12                           NotStarted    True                                     Move-VM -Compatibi...
    21026
    24000
    33012
    33012
    13     Job13                           NotStarted    True                                     Move-VM -Compatibi...
    
    
    
    PS C:\> Get-Job
    
    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
    --     ----            -------------   -----         -----------     --------             -------                  
    11     Job11                           Running       True            3173R03-32               Move-VM -Compatibi...
    12     Job12                           Running       True            3173R03-32               Move-VM -Compatibi...
    13     Job13                           Running       True            3173R03-32               Move-VM -Compatibi...
    
    
    

    VMs moving

    Nics moved to dummy vSwitch on source host

    dummy vswitch on source with same name as destination

    But disconnected in VM (so outage)

    No network in VM until move completed to real switch at destination

    On destination they will attach to the real vSwitch1 on the destination host.

    • Proposed as answer by NeighborGeek Monday, April 8, 2019 7:13 PM
    Wednesday, May 2, 2018 1:15 AM
  • Somebody get this guy a cold beer.  Thanks for coming back to this issue, armin19.  

    That's solid.  

    danielkr

    Tuesday, April 2, 2019 8:34 AM
  • Somebody get this guy a cold beer.  Thanks for coming back to this issue, armin19.  

    That's solid.  

    danielkr

    blast from the past!
    Monday, April 8, 2019 6:47 PM
  • Hi Armin,

    I hoped that someone else would have this pain!! :D

    I am migrating from 2012 failover to 2019 failover clusters, apparently MS cluster migration tool fails bad while doing that so I decided to script my way similarly like yourself however I am damn stuck at point 3 as I don't want a network outage!!! :)

    "The GUI actually makes the change at time of migration - only 1 ping is lost.  I have NOT found a powershell way to do this."

    This is exactly what I am trying to figure out... how does that work and how can I let's say ignore these exceptions when I try to move Old to New

    Here's my script:

    $SourceVolume = "C:\ClusterStorage\VOL-1"
    $SourceHost = ""
    $DestinationVolume = "Volume6"
    $DestinationHost = "HOST1111"
    $VMList = Import-Csv "C:\MoveVMS\VMList.csv"
    
    foreach ($VM in $VMList){
    #Get-VM -Name $VM.Name | Get-VMNetworkAdapter -Name $_.Name | select VMname,SwitchName
    Disconnect-VMNetworkAdapter -VMName $VM.Name
    Remove-ClusterGroup -VMId (Get-VM -Name $VM.Name).VMId -RemoveResources -Confirm:$false -Force
    New-Item -Path "\\$DestinationHost\C$\ClusterStorage\$DestinationVolume\$($VM.Name)" -ItemType Directory
    Move-VM -Name $VM.Name `
            -DestinationHost $DestinationHost `
            -IncludeStorage `
            -DestinationStoragePath "\\$DestinationHost\C$\ClusterStorage\$DestinationVolume\$($VM.Name)"
    }

    The thing is, on the new hyper-v host I have a different switch name i.e. 10gigconvergedswitch something and what I am missing in the script is to add it to that switch, add vlan, add it to cluster which is fine I will eventually get that done, but I want to figure out this bit first.

    So.... bump, anyone? :)

    Cheers,

    Vlatko


    Wednesday, December 25, 2019 1:28 PM
  • sorry it's so late, haven't been on here for a while.

    while this thread was really about migrating multiple VDHs for the same VM to different destinations, the network issue you describe can be deal with. as i said initially though, it's really much easier to just rename the vSwitch on the destination host. you can always change it back once the migrations are completed.

    at my old job i figured it out without renaming the vSwitches using this post and eventually this post if you're not using access and need VLANs.  we had SCVMM so at the end of the migration we would have an additional step of getting the VM onto the right VM Network after but it would communicate throughout the whole process.

    hope this helps.

    Friday, February 7, 2020 11:35 PM
  • sorry for digging out an old thread. But i had the same issue with migrating from an old cluster to a new one with changing network and vlan without causing downtime.

    so i found the altaro guide and some old stuff from Ben Armstrong but i couldn't get it working as described. The errors were the same as TolliLF described ("Connect-VMNetworkAdapter : Hyper-V was unable to find a virtual switch with name"). So i was really confused why this shouldn't work as all blogs describe the same.

    The solution was to not change the Incompatibilities.Sources but the VM.NetworkAdapters directly. With that the VM was still running with the old config and i could migrate it without causing to much downtime (only 2 dropped pings)

    Just want to make sure that anybody who finds this thread, like me, also finds the solution for the network change.

    hope i can help somebody with it :)

    Thursday, July 30, 2020 7:29 AM