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