Moving virtual machine without using Hyper-V export/import.

Rate this item
(0 votes)

@ECHO OFF

rem The script for English version of Hyper-V server
rem Allows to connect VM to Hyper-V with all snapshots and virtual hard drives without exporting.
rem apart from NTFS security on the basis of accounts and groups, Hyper-V sets security settings for files and virtual machines
rem this script makes a hard link for virtual machine XML file and places it in c:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\
rem and provides necessary permissions to this hard link and files of virtual machine.
rem in menu item 1 adds VM manually, item 2 adds manually VM snapshots, item 3 adds VM automatically

::---------- path to Hyper-V file where links are stored:
set HYPER-V_PATH=c:\ProgramData\Microsoft\Windows\Hyper-V
::---------- variable containing VM ID
set VM_ID=""
::---------- variable containing path to VM
set VM_PATH=""
::---------- variable containing VM snapshot ID
set SNAP_ID=""

:MAINMENU
set answer=""
cls
echo -------------------------------------------------
echo Menu
echo -------------------------------------------------
echo 1: Make Hard Links for Virtual Machine
echo 2: Make Hard Links for Virtual Machine Snapshots
echo 3: Make Hard Links for VM and Snapshots Automatic
echo 4: Exit
echo -------------------------------------------------
set /p answer=Enter Menu Item:
if %answer% equ 1 goto MENUVM
if %answer% equ 2 goto MENUSNAP
if %answer% equ 3 goto MENUAUTO
if %answer% equ 4 (
%~d0
goto :EOF )
else goto MAINMENU


::----- if ONE is pressed then set a HardLink for VM
:MENUVM
set /p VM_ID=Enter Virtual Machine GUID:
set /p VM_PATH=Enter path to Virtual Machine:
%VM_PATH:~0,2%

:: -----Call the procedure for adding virtual machine
call :add_vm_id
goto MAINMENU


::----- if TWO is pressed then set a HardLink for VM snapshot (assuming that VM is already registered, otherwise icacls will be incorrect)

:MENUSNAP
if %VM_ID%=="" set /p VM_ID=Enter Virtual Machine GUID:
if %VM_PATH%=="" set /p VM_PATH=Enter path to Virtual Machine:
%VM_PATH:~0,2%
set /p SNAP_ID=Enter Snapshot GUID:

:: -----Call add snapshot procedure
call :add_snapshot
goto MAINMENU


::----- THIRD menu item, VM and snapshots HardLink automatically on the way to folder containing VM
:MENUAUTO
set /p VM_PATH=Enter path to Virtual Machine(e.g.: d:\VMs\VM1):
%VM_PATH:~0,2%

:: ----- Search xml file in the folder %VM_PATH%\Virtual Machines and place it in VM_ID variable
for %%I in ("%VM_PATH%\Virtual Machines\*.xml") do set VM_ID=%%~nI
:: -----Call add VM procedure
call :add_vm_id
:: ----- Search xml fies in the folder %VM_PATH%\Snapshots and place every file found in SNAP_ID variable and register it
for %%K in ("%VM_PATH%\Snapshots\*.xml") do (
set SNAP_ID=%%~nK
call :add_snapshot
)
goto MAINMENU

:: ----- END of main module


:: -----the PROCEDURE for adding virtual machine
:add_vm_id
echo _
echo -=ADD VM=-
echo _
if exist "%VM_PATH%\Virtual Machines\%VM_ID%.xml" (
:: ----- Stop the Hyper-V virtual machine management service
net stop vmms
:: ----- if you find previous hard links, delete them (required when using Backup Exec)
if exist "%HYPER-V_PATH%\Virtual Machines\%VM_ID%.xml" del /F /Q "%HYPER-V_PATH%\Virtual Machines\%VM_ID%.xml"
:: ----- create hard link for XML file of VM snapshot
mklink /H "%VM_ID%.xml" "%VM_PATH%\Virtual Machines\%VM_ID%.xml"
:: ----- move hard link to where it belongs
move /Y "%VM_ID%.xml" "%HYPER-V_PATH%\Virtual Machines\"
:: ----- run Hyper-V virtual machine management service (required for VM ID to get registered and for icalcs to work correctly)
net start vmms
:: ----- set security permissions for contents of the folder Virtual Machines
icacls "%VM_PATH%\Virtual Machines\*" /grant "NT VIRTUAL MACHINE\%VM_ID%":F /T
:: ----- set security permissions for contents of the folder Virtual Hard Disks
icacls "%VM_PATH%\Virtual Hard Disks\*" /grant "NT VIRTUAL MACHINE\Virtual Machines":F /T
:: ----- set the necessary NTFS security for hard link
icacls "%HYPER-V_PATH%\Virtual Machines\%VM_ID%.xml" /grant "NT VIRTUAL MACHINE\%VM_ID%":F /T
echo _
echo VM %VM_ID% was added.
echo _
pause
exit /b
) else (
echo ERROR: VM XML-file not found! Try one more time...
pause
%~d0
exit
)

:: -----the PROCEDURE for adding a snapshot

:add_snapshot
echo _
echo -=ADD SNAPSHOT=-
echo _
if exist "%VM_PATH%\Snapshots\%SNAP_ID%.xml" (
:: ----- if you find previous hard links, delete them (required when using Backup Exec)
if exist "%HYPER-V_PATH%\Snapshots\%SNAP_ID%.xml" del /F /Q "%HYPER-V_PATH%\Snapshots\%SNAP_ID%.xml"
:: ----- create hard link for XML file of VM snapshot
mklink /H "%SNAP_ID%.xml" "%VM_PATH%\Snapshots\%SNAP_ID%.xml"
:: ----- move hard link to where it belongs
move /Y "%SNAP_ID%.xml" "%HYPER-V_PATH%\Snapshots\"
:: ----- set security permissions for contents of the folder Snapshots
icacls "%VM_PATH%\Snapshots\*" /grant "NT VIRTUAL MACHINE\%VM_ID%":F /T
:: ----- set the necessary NTFS security for hard link
icacls "%HYPER-V_PATH%\Snapshots\%SNAP_ID%.xml" /grant "NT VIRTUAL MACHINE\%VM_ID%":F /T
echo _
echo SHAPSHOT %SNAP_ID% was added.
echo _
pause
exit /b
) else (
echo ERROR: Snapshot XML-file not found! Try one more time...
pause
%~d0
exit
)

Last modified on Thursday, 28 May 2015 15:46
Data Recovery Expert

Viktor S., Ph.D. (Electrical/Computer Engineering), was hired by DataRecoup, the international data recovery corporation, in 2012. Promoted to Engineering Senior Manager in 2010 and then to his current position, as C.I.O. of DataRecoup, in 2014. Responsible for the management of critical, high-priority RAID data recovery cases and the application of his expert, comprehensive knowledge in database data retrieval. He is also responsible for planning and implementing SEO/SEM and other internet-based marketing strategies. Currently, Viktor S., Ph.D., is focusing on the further development and expansion of DataRecoup’s major internet marketing campaign for their already successful proprietary software application “Data Recovery for Windows” (an application which he developed).

18 comments

  • Comment Link Andrew Friday, 15 December 2017 14:01 posted by Andrew

    This is my first time pay a visit at here and i am really happy
    to read all at one place.

  • Comment Link Rosie Monday, 27 November 2017 16:09 posted by Rosie

    Thank you for the auspicious writeup. It in reality
    used to be a entertainment account it. Glance complicated to far brought agreeable
    from you! By the way, how could we be in contact?

  • Comment Link Magda Monday, 20 November 2017 23:24 posted by Magda

    It's appropriate time to make some plans for the future and it
    is time to be happy. I've read this post and if I could I want to suggest
    you few interesting things or suggestions. Maybe you
    could write next articles referring to this
    article. I wish to read more things about it!

  • Comment Link Gisele Monday, 13 November 2017 19:32 posted by Gisele

    Do you experience feeling these is often more plug on your property?

  • Comment Link Elliott Saturday, 11 November 2017 19:50 posted by Elliott

    Have you been currently than needed and your loved ones?

  • Comment Link Dannie Wednesday, 08 November 2017 18:18 posted by Dannie

    Link exchange is nothing else except it is only placing the other person's webpage link on your page at suitable place and other
    person will also do similar in support of you.

  • Comment Link Klaudia Tuesday, 07 November 2017 21:42 posted by Klaudia

    I'm really enjoying the design and layout of your website.
    It's a very easy on the eyes which makes it much more pleasant for me to come here and
    visit more often. Did you hire out a developer to create your theme?
    Great work!

  • Comment Link Miquel Friday, 03 November 2017 19:05 posted by Miquel

    For most up-to-date news you have to go to see web and on world-wide-web I
    found this web site as a most excellent site for most up-to-date updates.

  • Comment Link Candelaria Thursday, 02 November 2017 19:52 posted by Candelaria

    Hi there i am kavin, its my first time to commenting
    anywhere, when i read this paragraph i thought i could also make comment due to this
    brilliant post.

  • Comment Link Claudia Thursday, 02 November 2017 00:34 posted by Claudia

    You really make it seem so easy with your presentation but I
    find this topic to be really something that I think I would never understand.
    It seems too complex and extremely broad for
    me. I am looking forward for your next post, I will try to get the
    hang of it!

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.