Of course, You should use this information on Your own risk, make backups and understand what you are doing.

Post is created under the impression of Alexander Kupchinets​ky’s post

In earlier versions(like Virtual Server 2005 R2),  when You need to move or restore virtual machine, You could only copy VHD and correct the config files. In Hyper-V these actions are not enough. If You want Your VM to be visible, You also need to create xml files(“hardlinks”), give permissions and provide it all to Hyper-V Services. I created two cmd files, which make this process easier:

@goto end_of_comments
————————————————————-
ATTACH Hyper-V VM

Variables:

VMFOLDER is virtual machine folder, without end slash

VMGUID is 32-digit number from ‘Virtual Machines’ folder,
example: for "D:\VM\Virtual Machines\9ACC595A-61B4-4BD7-A46B-67D5B20E38E6.xml" folder
it is 9ACC595A-61B4-4BD7-A46B-67D5B20E38E6
————————————————————-
:end_of_comments

rem set variables
set vmguid=9ACC595A-61B4-4BD7-A46B-67D5B20E38E6
set vmfolder=D:\VM

rem stop Hyper-V services
net stop "Hyper-V Image Management Service"
net stop "Hyper-V Networking Management Service"
net stop "Hyper-V Virtual Machine Management"

rem configure Hyper-V hardlinks and set permissions
mklink "C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\%vmguid%.xml" "%vmfolder%\Virtual Machines\%vmguid%.xml"
cacls "C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\%vmguid%.xml" /L /E /G "NT VIRTUAL MACHINE\%vmguid%":F
cacls "%vmfolder%" /T /E /G "NT VIRTUAL MACHINE\%vmguid%":F

net start "Hyper-V Virtual Machine Management"
net start "Hyper-V Networking Management Service"
net start "Hyper-V Image Management Service"

Here You need to set two variables:
here1_attach_vm_scr1
First – is 32-digit GUID of Virtual machine. You can find it in “Virtual Machines” folder, and the second parameter is source folder, where Your Virtual machine lives, without last slash(“D:\VM” here).

After cmd work completed, VM appears in Virtual Machine Manager.

If Your VM has snapshots, You need to attach everyone of them too. Here is cmd for snapshot(here You’ll need to set snaphot GUID too, and repeat for every snapshot):

@goto end_of_comments
ATTACH Hyper-V SHAPSHOT

Variables:

VMFOLDER is virtual machine folder without end slash
VMGUID is 32-digit number from ‘Virtual Machines’ folder,
SNAPSHOT is 32-digit number from ‘Snapshots’ folder
————————————————————-
:end_of_comments

rem set variables
set vmfolder=D:\VM\Win2008r2-1
set vmguid=76DA3C83-11D0-4FF4-B909-3E7CC40C0D3C
set snapshot=5E688829-B1BF-47A2-A1A8-2370EE163F71

rem stop Hyper-V services
net stop "Hyper-V Image Management Service"
net stop "Hyper-V Networking Management Service"
net stop "Hyper-V Virtual Machine Management"

rem configure Hyper-V hardlinks and set permissions
mklink "C:\ProgramData\Microsoft\Windows\Hyper-V\Snapshots\%snapshot%.xml" "%vmfolder%\Snapshots\%snapshot%.xml"
cacls "C:\ProgramData\Microsoft\Windows\Hyper-V\Snapshots\%snapshot%.xml" /L /E /G "NT VIRTUAL MACHINE\%vmguid%":F
cacls "%vmfolder%" /T /E /G "NT VIRTUAL MACHINE\%vmguid%":F

net start "Hyper-V Virtual Machine Management"
net start "Hyper-V Networking Management Service"
net start "Hyper-V Image Management Service"

Tested with Service Center Virtual Machines Manger Admin Console, with remote Virtual Machines hosted on Windows 2008 Hyper-V R2. What is interesting that start/stop Hypervisor services does not affect other working virtual machines, so You can move/restore virtual machine “on a hot” server with working virtual machines

Click below to expand comments form..