Windows cmd批处理脚本,一键创建/挂载/格式化/分离,VHD虚拟硬盘

3,667次阅读

共计 1772 个字符,预计需要花费 5 分钟才能阅读完成。

@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (goto UACPrompt) else (goto gotAdmin)
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "","", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" (del "%temp%\getadmin.vbs")
if exist %tmp%\vhdconf (del /f /q %tmp%\vhdconf)
title Www.ZhiQiang.Name
mode con: cols=45 lines=10
color 2F
pushd "%~dp0"
for /f "delims=" %%i in ('dir /b %~dp0^|findstr".*\.vhd"') do (set name=%%i)
:menu
cls
echo.
echo 1. 挂载 %name%
echo.
echo 2. 分离 %name%
echo.
echo 3. 创建 VHD
echo.
echo 4. 格式化 %name%
echo.
choice /C:1234 /N /M "请输入你的选择 [1,2,3,4]":if errorlevel 4 goto:vhdformat
if errorlevel 3 goto:vhdadd
if errorlevel 2 goto:vhdoff
if errorlevel 1 goto:vhdon
:vhdon
echo SELECT VDISK FILE= "%~dp0%name%" >%tmp%\vhdconf
echo ATTACH VDISK NOERR >>%tmp%\vhdconf
echo LIST DISK >>%tmp%\vhdconf
DISKPART /s %tmp%\vhdconf > nul
exit
:vhdoff
echo SELECT VDISK FILE= "%~dp0%name%" >%tmp%\vhdconf
echo ATTACH VDISK NOERR >>%tmp%\vhdconf
echo DETACH VDISK >>%tmp%\vhdconf
echo LIST DISK >>%tmp%\vhdconf
DISKPART /s %tmp%\vhdconf > nul
exit
:vhdadd
cls
echo.
echo 自定义 vhd 虚拟硬盘文件名称与大小, 按回车
echo.
set /p newname= 文件名:echo.
set /p value= 大小 (G):
set /a mx = (value)*(1024)
echo CREATE VDISK FILE="%~dp0%newname%.vhd" TYPE=FIXED MAXIMUM=%mx% >>%tmp%\vhdconf
echo SELECT VDISK FILE="%~dp0%newname%.vhd" >>%tmp%\vhdconf
echo ATTACH VDISK NOERR >>%tmp%\vhdconf
echo CLEAN >>%tmp%\vhdconf
echo CREATE PARTITION PRIMARY >>%tmp%\vhdconf
echo FORMAT FS=NTFS QUICK >>%tmp%\vhdconf
echo ASSIGN >>%tmp%\vhdconf
DISKPART /s %tmp%\vhdconf > nul
exit
:vhdformat
echo SELECT VDISK FILE="%~dp0%name%" >>%tmp%\vhdconf
echo ATTACH VDISK NOERR >>%tmp%\vhdconf
echo CLEAN >>%tmp%\vhdconf
echo CREATE PARTITION PRIMARY >>%tmp%\vhdconf
echo FORMAT FS=NTFS QUICK >>%tmp%\vhdconf
echo ASSIGN >>%tmp%\vhdconf
DISKPART /s %tmp%\vhdconf > nul
exit

正文完
 0
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 于2019-09-02发表,共计1772字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。