|
本帖最后由 oym2016 于 2025-3-15 08:07 编辑
初学 deepseek_r1应用于批处理:@echo off
setlocal enabledelayedexpansion
:: 检查管理员权限
fltmc >nul 2>&1 || (
echo 必须使用管理员权限运行此脚本!
pause
exit /b 1
)
echo.&echo "VHD虚拟系统安装批处理(需管理员身份运行):"
::选择磁盘
:input_drive
echo.&set /p "vhd_fd=请输入存放VHD的盘符(如 C D E ...): "
set "vhd_fd=%vhd_fd::=%"
if not exist "!vhd_fd!:\" (
echo 错误:盘符无效
timeout /t 2 >nul
goto input_drive
)
::设置名称
:input_name
echo.&echo 字样:仅供复制粘贴("旗舰版=Ultimate" "企业版=Enterprise" "专业版=Professional")
set vhd_fnx=Win10Pro.vhdx
echo.&set /p "vhd_fnx=输入VHD名称和扩展名(默认: Win10Pro.vhdx) "
set "vhd_fnx=%vhd_fnx: =%"
for %%a in ("%vhd_fnx%") do (
set "vhd_fx=%%~xa"
set "vhd_fp=%%~na"
)
if /i not "%vhd_fx%"==".vhd" if /i not "%vhd_fx%"==".vhdx" (
echo 错误:仅支持.vhd或.vhdx文件
goto :input_name
)
::唯一名称
set vhd_dir=!vhd_fd!:\!vhd_fp!
set "vhd_path=!vhd_dir!\!%vhd_fnx!"
set "vhd_path=%vhd_path: =%"
if exist "!vhd_path!" (
set "cnt=1"
:name_check
set "vhd_path=!vhd_dir!\!vhd_fp!_!cnt!!vhd_fx!"
if exist "!vhd_path!" (set /a cnt+=1 & goto name_check)
)
if not exist "%vhd_dir%" md "%vhd_dir%"
::设置格式及大小
:size
set type=expandable
if /i "%vhd_fx%" == ".vhdx" (
set mb=gpt
)else (
set mb=mbr
)
if "%value%"=="" set "value=32"
echo.&set /p "value=设置(!vhd_fnx!)容量大小(默认:32G) "
set /a mx=value*1024
echo %value%|findstr /r "^[0-9]*$" >nul || (echo 无效数字&goto :size)
echo.&echo VHD磁盘参数如下:
echo.&echo 创建动态磁盘:"!vhd_path!"
echo.&echo 磁盘容量: %value% GB (%mx% MB)
echo.&pause
cls
::创建和执行diskpat脚本
for %%i in (R S T U V W Y Z A B C D E F G H I J K L M N O P Q) do (
if not exist %%i:\ (
set "pan=%%i"
goto :assign_vhd
)
)
:assign_vhd
echo create vdisk file="%vhd_path%" maximum=%mx% type=%type% > "%temp%\vhd.txt"
echo attach vdisk >> "%temp%\vhd.txt
echo select vdisk file="%vhd_path%" >> "%temp%\vhd.txt"
echo clean >> "%temp%\vhd.txt"
echo convert %mb% >> "%temp%\vhd.txt"
echo create partition primary >> "%temp%\vhd.txt"
echo format fs=ntfs label="%vhd_fp%" quick >> "%temp%\vhd.txt"
echo assign letter=%pan%: >> "%temp%\vhd.txt"
diskpart /s "%temp%\vhd.txt"
del "%temp%\vhd.txt" >nul 2>&1
echo.&echo.
if exist !pan!:\ (
echo VHD成功创建并挂载为!pan!:盘
) else (
echo VHD挂载失败,请检查diskpart输出!
pause
exit /b 1
)
echo.&pause
::设置BCD
:set_bcd
cls
:user_ext
if exist s: mountvol s: /d >nul
mountvol s: /s >nul&& (set ext=efi) || (set ext=exe)
if exist s: mountvol s: /d >nul
:bcd_title
echo.&echo 字样:仅供复制粘贴("旗舰版=Ultimate" "企业版=Enterprise" "专业版=Professional")
set "bcd_title=!vhd_fnx!"
echo.&set /p "bcd_title=BCD菜单标题(默认 %vhd_fp%): "
if "!bcd_title!"=="" set "bcd_title=%vhd_fp%"
echo.
echo BCD参数:
echo 设备文件: [!vhd_path:~0,2!]!vhd_path:~2!
echo 系统存储: SystemBCD
echo 启动文件: \Windows\system32\winload.%ext%
echo 菜单标题: %bcd_title%
echo.&pause
::创建BCD条目
:set_guid
for /f "tokens=2 delims={}" %%c in ('bcdedit /create /d "%bcd_title%" /application OSLOADER 2^>^&1 ^| find "{"') do set "id={%%c}"
bcdedit /set %id% device "vhd=[!vhd_path:~0,2!]!vhd_path:~2!"
bcdedit /set %id% OSdevice "vhd=[!vhd_path:~0,2!]!vhd_path:~2!"
bcdedit /set %id% path "\windows\system32\winload.%ext%"
bcdedit /set %id% systemroot \windows
bcdedit /displayorder %id% /addlast
bcdedit /set {bootmgr} default %id%
bcdedit /timeout 5
echo.&echo BCD条目已创建:!id!
::生成删除脚本
echo bcdedit /delete %id% > "%vhd_dir%\Del_%vhd_fp%_GUID.cmd"
echo del "%%~f0" >> "%vhd_dir%\Del_%vhd_fp%_GUID.cmd"
if exist "%vhd_dir%\Del_%vhd_fp%_GUID.cmd" (
echo 删除BCD菜单中 %vhd_fp%启动条目生成.
echo Del_%vhd_fp%_GUID.cmd 放在%vhd_dir%目录下.
)
echo.&pause
::选择操作
cls
:user_choice
echo.&echo 选择操作...
echo.&echo 1.分离VHD并退出 2.安装或还原系统到VHD
echo.&set /p "user_select=请输入数字选择操作: "
if "%user_select%"=="1" goto vhd_detach
if "%user_select%"=="2" goto vhd_install
echo 错误:无效的选择!
timeout /t 2 >nul
goto user_choice
::安装系统
:vhd_install
echo.&echo 安装系统...
set /p "wim=请将WIM/ESD文件拖入窗口或输入路径: "
set "wim=!wim:"=!"
call :ChoiseIndex !wim!
echo.&Dism.exe /apply-image /imagefile:"!wim!" /index:!index! /applydir:!pan!:\
::应答文件
:Unattend
echo.&set "targetDir=%pan%:\Windows\Panther"
echo.&set "xml=%targetDir%\Unattend.xml"
if not exist "%xml%" (
if not exist "%targetDir%\" mkdir "%targetDir%"
(
echo ^<?xml version="1.0" encoding="utf-8"?^>
echo ^<unattend xmlns="urn:schemas-microsoft-com:unattend"^>
echo ^<settings pass="oobeSystem"^>
echo ^<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"^>
echo ^<OOBE^>
echo ^<SkipMachineOOBE^>true^</SkipMachineOOBE^>
echo ^<SkipUserOOBE^>true^</SkipUserOOBE^>
echo ^</OOBE^>
echo ^<UserAccounts^>
echo ^<AdministratorPassword^>
echo ^<Value/^>
echo ^<PlainText^>true^</PlainText^>
echo ^</AdministratorPassword^>
echo ^</UserAccounts^>
echo ^<AutoLogon^>
echo ^<Username^>Administrator^</Username^>
echo ^<Password^>
echo ^<Value/^>
echo ^<PlainText^>true^</PlainText^>
echo ^</Password^>
echo ^<Enabled^>true^</Enabled^>
echo ^<LogonCount^>1000000^</LogonCount^>
echo ^</AutoLogon^>
echo ^</component^>
echo ^</settings^>
echo ^</unattend^>
) > "%xml%"
)
echo.&if exist %xml% echo 无人值守文件已创建: %xml%
::安装驱动
:Add-Driver
echo.
echo.&echo 注意: 直接回车取消安装驱动 分离VHD并退出.
echo.&set /p "back_Driver=请输入或拖拽备份驱动目录路径: "
if defined back_Driver (
if exist "!back_Driver!\" (
echo.&Dism /Image:%pan%: /Add-Driver /Driver:"!back_Driver!" /Recurse /ForceUnsigned
) else (
echo 目录不存在,跳过驱动安装。
)
)
::分离退出
:vhd_detach
echo.&echo 按任意键...分离VHD(此时可用第三工具优化系统再分离)...&pause >nul
(echo select vdisk file="%vhd_path%"&echo detach vdisk)|diskpart
:end
endlocal& exit
:ChoiseIndex
setlocal enabledelayedexpansion
set "wim_file=%~1"
set index=1
set count=0
echo 正在获取镜像信息...
for /f "tokens=2 delims=: " %%i in ('Dism /Get-ImageInfo /ImageFile:"!wim_file!" ^| findstr /i "索引"') do (
set /a count+=1
set "index_list=!index_list! %%i"
echo 索引 %%i
)
if !count! gtr 1 (
echo.
set /p "index=请选择镜像的索引(1-!count!):"
) else (
set index=1
)
endlocal & set index=%index%
goto :eof
|
|