Thursday, 28 May 2015 00:00

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).

Leave a comment

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

Get Help Now

Thank you for contacting us.
Your Private Investigator will call you shortly.