共用方式為


Hyper-V: Migrate the Hyper-V Role

 

Applies To: Windows Server 2008 R2, Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 with SP1

Migrate the Hyper-V Role

The steps to migrate the Hyper-V role are the same for all of the scenarios defined in “Supported migration scenarios,” earlier in this guide.

Perform migration steps on the source server

To perform migration steps on the source server
  1. Prepare the virtual machines for migration by shutting down the virtual machines.

  2. Open a Windows PowerShell session with elevated user rights by doing one of the following:

    • To run Windows PowerShell as an administrator from the Start screen, right-click the Windows PowerShell tile, and in the app bar, click Run as administrator.

    • To run Windows PowerShell as an administrator from the desktop, right-click the Windows PowerShell shortcut in the taskbar, and then click Run as Administrator.

  3. Load Windows Server Migration Tools into your Windows PowerShellsession.

    Only load the Windows Server Migration Tools snap-in into a Windows PowerShell session that was opened by using some other method (and into a session where it has not already been loaded). To load Windows Server Migration Tools, type the following, and then press ENTER.

    Add-PSSnapin Microsoft.Windows.ServerManager.Migration
    
  4. From Windows PowerShell, collect data from the source server by running the Export-SmigServerSetting cmdlet as an administrator. The Export-SmigServerSetting cmdlet creates an XML file, StoragePathMappings.xml, that contains information about where the virtual machine storage (.vhd and .avhd) files are stored, in the form of folder paths on the source server. The import process uses the StoragePathMappings.xml file to associate the storage files to the appropriate virtual machines on the destination server. If the destination server will use the same drive mapping and folder structure for virtual machine storage as the source server, you do not need to edit the file after the Export-SmigServerSetting cmdlet creates it. Otherwise, you must edit this file before you import it to the destination server. Before you run this command, review the following information to determine the information to include in the command:

    • Determine where to store StoragePathMappings.xml. The -Path parameter is a required parameter that specifies the location. You can either choose a location that can be accessed by both the source server and the destination server, such as a network location, or copy it from the source server to the destination server.

    • To export user groups, which are used for access control by the Hyper-V security policy and the Hyper-V remote management tools, include the -User and -Group parameters:

      -User <Enabled | Disabled | All> -Group
      
    • To recreate the same IP settings on the physical network adapters on the destination server as are configured on the source server, include the -IPConfig parameter. The -IPConfig parameter collects IP information when it is used with the Export-SmigServerSetting cmdlet on the source server. The -IPConfig parameter applies settings when the Import-SmigServerSetting cmdlet is used on the destination server.

      - HYPERLINK "https://technet.microsoft.com/en-us/library/dd379503(WS.10).aspx" IPConfig
      
  5. After you determine the parameters, run the Export-SmigServerSetting cmdlet, where <storepath> specifies the path to the folder where the configuration data file (Svrmig.mig) will be stored. (For example, C:\Migration.) The StoragePathMappings.xml file is created in a subfolder of the <storepath> folder, named VirtualMachines. (For example, C:\Migration\VirtualMachines.)

    Export-SmigServerSetting -FeatureId Hyper-V -IPConfig -User All -Group -path <storepath> -Verbose
    

Migrate virtual machine data

The following steps show you how to use Robocopy and a Windows PowerShell script to copy the data from the source server to the destination server. The script parses the folder paths specified in the StoragePathMappings.xml to migrate the data. You can use the StoragePathMappings.xml file stored under <storepath> as a reference to determine which folders need to be transferred.

Important

If you want to use a different drive mapping and/or folder structure on the destination server, edit the StoragePathMappings.xml file before you attempt to migrate the data to the destination server.

To migrate virtual machine data
  1. Copy the data from the source server to the destination server. The recommended way to do this is to use the Robocopy command. You can run the command for each file and specify the source and destination locations. (You can use the StoragePathMappings.xml file to determine the source paths.) Or, you can automate this process by using a Windows PowerShell script. Before you run the script, update the StoragePathMappings.xml file with the locations where you want to paste the files on the destination server. The script can parse the StoragePathMappings.xml file and then call the Robocopy command to copy and paste the files.

    The following is an example of such a script. To use this sample, copy the code and paste it into a text editor, then save the file with a .ps1 file name extension in a directory where you want to run the script from. For example, save CopyData.ps1 to C:\migration\.

    param(
        [string]$xmlFilePath = $(throw "Must pass the fully qualified file name of Storage Path XML in the command string"),
        [string]$destinationHost = $(throw "Must pass the Destination Host Name (NetBiosName), where the files will be copied to")
    )
    
    Write-Host "XML File Path: " $xmlFilePath
    Write-Host "Destination Host Name: " $destinationHost
    
    # Get the content of the XML file
    [xml]$xmlFile = Get-content $xmlFilePath
    
    # For each storage path, if the "Copy" attribute is true copy the files to the destination Host.
    foreach ($storagePath in $xmlFile.StoragePaths.storagePath)
    {
             if($storagePath.Copy -eq "true")
             {
                    # Get the Source directory
                    $sourceDirectory = $storagePath.Source
    
                    # Get the Destination directory
                    $destinationDirectory = $storagePath.Destination
                    $destinationDirectory = $destinationDirectory -replace ":","$"
                    $destinationDirectory = "\\" + $destinationHost + "\" + $destinationDirectory
    
                    # Copy the files to the destination host
                    robocopy $sourceDirectory $destinationDirectory /E /XF *.xml /R:5 /W:60 /V
        }
    }
    

    To run the script, type the full path to the script at the command prompt and pass the fully qualified file name of the StoragePathMappings.xml file (full path and file name) and the name of the destination server as parameters. The file name extension of the script is optional. For more information, see Support for Scripting (https://go.microsoft.com/fwlink/?LinkID=178144).

    For example, if you used the folder and file name example shown above, type:

    c:\migration\copyData.ps1 <XMLPathName> <DestinationServerName>
    
  2. Disconnect the source server from the network so that you avoid any potential MAC address conflicts between the virtual machines on the source and destination servers. MAC address conflicts may impact the availability of the workloads that run on the virtual machines.

Perform migration steps on the destination server

  1. If the <storepath> is located anywhere other than locally on the destination server, edit the permissions of the shared folder to grant Full Control to the following accounts:

    • The user account that will run the import and export commands. If the same account is used, only one entry is required.

    • The computer account of the source server.

    • The computer account of the destination server.

  2. If you used another method instead of the Robocopy command to copy data to the destination server, check the destination folder and delete any .xml files that were copied to that folder.

  3. Open a Windows PowerShell session with elevated user rights by doing one of the following:

    • To run Windows PowerShell as an administrator from the Start screen, right-click the Windows PowerShell tile, and in the app bar, click Run as administrator.

    • To run Windows PowerShell as an administrator from the desktop, right-click the Windows PowerShell shortcut in the taskbar, and then click Run as Administrator.

  4. Load Windows Server Migration Tools into your Windows PowerShellsession.

    Only load the Windows Server Migration Tools snap-in into a Windows PowerShell session that was opened by using some other method (and into a session where it has not already been loaded). To load Windows Server Migration Tools, type the following, and then press ENTER.

    Add-PSSnapin Microsoft.Windows.ServerManager.Migration
    
  5. To import the Hyper-V settings to the destination server, run the Import-SmigServerSetting cmdlet and all additional parameters that you used with the Export-SmigServerSetting.

    Import-SmigServerSetting -FeatureId Hyper-V <additionalparameters> -path <storepath> -Verbose -Force
    

    Additional parameters:

    • To import the same IP settings on the destination server that were on the source server, where <SourcePhysicalAddress-1> and <SourcePhysicalAddress-2> are comma-separated lists of the physical addresses of the source network adapter, and <TargetPhysicalAddress-1> and <TargetPhysicalAddress-2> are comma-separated lists of the physical addresses of the destination network adapter, include:

      -IPConfig All -SourcePhysicalAddress "<SourcePhysicalAddress1>","<SourcePhysicalAddress2>" -TargetPhysicalAddress "<TargetPhysicalAddress1>","<TargetPhysicalAddress2>"
      
    • To import the user groups that are used by the Hyper-V security policy and remote administration, include:

      -User <Enabled | Disabled | All> -Group
      
  6. If a failure occurred while running the Import-SmigServerSetting cmdlet, review the Setupact.log and Setuperr.log under %localappdata%\SvrMig\Log.

  7. Use the information you gathered about physical-to-virtual network connections to establish the connections between the physical network adapters and external virtual switches on the destination server. (Virtual networks are now referred to as virtual switches in Windows Server 2012.) All external virtual networks are migrated to the destination server as internal virtual switches because the import process cannot map virtual switches to physical networks. To establish the connections:

    1. Open Hyper-V Manager. (From the Start screen, click the Hyper-V Manager tile.)

    2. In the Action pane, click Virtual Switch Manager.

    3. In the left pane, under Virtual Switches, click the name of the first internal switch that you want to convert to an external switch.

    4. In the right pane, under Connection type, select External. From the drop-down list, select the physical network adapter to use for access to the physical network.

    5. Click OK to save the changes and close Virtual Switch Manager, or click Apply to save the changes and modify another virtual switch.

  8. For each virtual machine that used a physical disk connected directly to the physical computer, establish this connection on the destination server:

    1. Open the Disk Management snap-in and verify that the disk is in an Offline state. If the disk is not in an Offline state, it will not be available when configuring storage for a virtual machine.

    2. Open Hyper-V Manager. (From the Start screen, click the Hyper-V Manager tile.)

    3. Under Virtual Machines, select the virtual machine that you want to connect to the physical disk.

    4. In the Action pane, under the virtual machine name, click Settings.

    5. In the navigation pane (left pane), click the controller that you want to attach the disk to. If you plan to use the disk as a startup disk, make sure you attach it to an IDE controller. Click Add.

    6. On the Hard Drive page, select the location on the controller to attach the disk.

    7. Under Media, specify the physical hard disk. If the disk does not appear in the drop-down list under Physical hard disks, make sure the disk is in an Offline state in Disk Management.

    8. Select Physical hard disk, and then click OK.

  9. Restore any customizations you made to the WMI namespace security settings. For more information, see WMI namespace security customizations are missing after upgrading to Windows Server 2008 R2(https://go.microsoft.com/fwlink/?LinkId=178143).

  10. If you turned off any exceptions in Windows Firewall on the source server, turn off those same exceptions on the destination server.

  11. If you have granted remote access to the server for Hyper-V management to any user account that is not a member of the Administrators group or Distributed COM Users group, open the registry and navigate to the HKLM\Software\Microsoft\OLE\ key. Add the MachineLaunchRestriction value that you recorded from the source server.

    Warning

    Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.