电脑城新晋技术一枚,为了提高效率,东拉西扯出来的批处理,基本可以正常运行。
求大神关注,优化,提供高见
实战环境:微pe2.2,官方系统镜像及常用软件- @echo off
- title ★★★ 系统安装脚本 ★★★
- :: Date: 2023-03-08
-
- ::自动请求以管理员权限运行
- if /i "%systemdrive%"=="C:" reg query HKU\S-1-5-20>nul 2>nul || echo;CreateObject^("Shell.Application"^).ShellExecute "%~f0", "%*", "", "runas", 1 > "%temp%\getadmin.vbs" && cscript //b "%temp%\getadmin.vbs" && exit /b & del "%temp%\getadmin.vbs" /f /q>nul
-
-
- SetLocal EnableDelayedExpansion
- :: 跳转到批处理所在目录
- set DIR_CUR=%~dp0
- cd %DIR_CUR%
- :: 确保choice在系统中,部分pe精简掉了
- copy %DIR_CUR%_DESKTOP\choice.exe %windir%\sysWOW64\ /y
- copy %DIR_CUR%_DESKTOP\choice.exe %windir%\system32\ /y
-
- :: 暴力更改盘符,防止后面分区、挂载镜像时盘符被占用,完成后重启批处理。此部分实际运行有问题,待修复
- if "%1" == "" (goto :Ycl) else (goto :Start)
-
- :Ycl
- echo 把CDZO盘符清理出来,防止影响批处理后面执行
- (echo sel vol=c & echo assign letter=p)|diskpart>nul
- (echo sel vol=d & echo assign letter=q)|diskpart>nul
- (echo sel vol=e & echo assign letter=r)|diskpart>nul
- (echo sel vol=z & echo assign letter=s)|diskpart>nul
- (echo sel vol=o & echo assign letter=t)|diskpart>nul
- echo 盘符清理完成,重启批处理
- choice /t 20 /d y /n >nul
- :: 查找当前批处理的新路径
- set "FileName=DeployOS.bat"
- echo 正在搜索,请稍候...
- for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
- if exist %%a:\ (
- if exist %%a:\%FileName% (set "NEWDEP=%%a:\%FileName%")
- )
- )
-
- start %NEWDEP% 6
- exit
-
-
- :Start
- :: 系统镜像文件
- set W10PROIMG=%DIR_CUR%_OS\WIN10_22H2.iso
- set W11PROIMG=%DIR_CUR%_OS\WIN11_22H2.iso
- set W10LTSCIMG=%DIR_CUR%_OS\WIN10_LTSC.iso
- :: 应答文件
- set UNATT10PRO=%DIR_CUR%_OS\Afiles\WIN10.xml
- set UNATT11PRO=%DIR_CUR%_OS\Afiles\WIN11.xml
- set UNATT10LTSC=%DIR_CUR%_OS\Afiles\WIN10.xml
- :: 默认等待时间
- set ETR=6
-
-
-
- :first
- cls
- color 1F
- echo.
- echo _____________________选择操作系统_____________________
- echo ^| ^|
- echo ^| [ 1 ] WIN10 专业版 ^|
- echo ^| [ 2 ] WIN11 专业版 ^|
- echo ^| [ 3 ] WIN10 LTSC 【默认选项】 ^|
- echo ^| [ 0 ] 返回 重新选择 ^|
- echo ^|______________________________________________________^|
- echo.
- echo 请输入操作系统 [1230],%ETR%秒后默认系统: 3
- choice /c 1230 /n>nul /t %ETR% /d 3
- echo 选择系统: %errorlevel%
- if %errorlevel%==4 (goto first)
- if %errorlevel%==3 (set "SEL_IMG=%W10LTSCIMG%" & set "SEL_IDX=1" & set "SEL_OS_NAME=Windows 10 LTSC" & set "UNATT=%UNATT10LTSC%")
- if %errorlevel%==2 (set "SEL_IMG=%W11PROIMG%" & set "SEL_IDX=4" & set "SEL_OS_NAME=Windows 11 Pro " & set "UNATT=%UNATT11PRO%")
- if %errorlevel%==1 (set "SEL_IMG=%W10PROIMG%" & set "SEL_IDX=4" & set "SEL_OS_NAME=Windows 10 Pro " & set "UNATT=%UNATT10PRO%")
- cls
- echo.
- echo _____________________选择分区方案_____________________
- echo ^| ^|
- echo ^| [ 1 ] 1分区,全部分配给C盘 ^|
- echo ^| [ 2 ] 2分区,C盘100G,其他给D盘 【默认选项】 ^|
- echo ^| [ 3 ] 3分区,C盘200G,D盘500G,其他给E盘 ^|
- echo ^| [ 0 ] 返回 重新选择 ^|
- echo ^|______________________________________________________^|
- echo.
- echo 准备安装 %SEL_OS_NAME%
- echo -----------------------------------------------------
- echo 请输入硬盘分区方案 [1230],%ETR%秒后默认分区方案: 2
- choice /c 1230 /n>nul /t %ETR% /d 2
- echo 选择方案: %errorlevel%
- if %errorlevel%==4 (goto first)
- if %errorlevel%==3 (set "CRT_PART_NAME=3分区,C盘200G D盘500G 其他给E盘" & set "CRT_PART=CP3")
- if %errorlevel%==2 (set "CRT_PART_NAME=2分区,C盘100G,其他给D盘 " & set "CRT_PART=CP2")
- if %errorlevel%==1 (set "CRT_PART_NAME=1分区,全部分配给C盘 " & set "CRT_PART=CPAll")
-
- :: 显示磁盘分区情况,测试盘符被修改后此部分会出错,待修复
- cls
- echo.
- echo =====================磁盘分区详情=====================
- echo.
- :: 磁盘总数,从0开始
- set /a "dn=0"
- for /f "delims=" %%i in ('echo list disk^|diskpart^|findstr /i "联机"') do (set /a dn+=1)
- set /a DSK_NO=%dn%-1
-
-
- :: 生成磁盘信息
- if exist dp_t.txt del dp_t.txt
- for /l %%i in (0,1,!DSK_NO!) do (
- (echo sel disk %%i & echo detail disk & echo list part)|diskpart>>dp_t.txt
- )
-
- ::解析内容,生成硬盘分区文件 [序号,磁盘号,分区号,大小,文件系统, 盘符,标签]
- SetLocal EnableDelayedExpansion
- for /f "delims=" %%j in (dp_t.txt) do (
- set "oneline=%%j"
- ::行号
- set /a l+=1
- ::echo 第!l!行: !oneline:~38,4!
- if "!oneline:~0,5!"=="在计算机上" (set /a n=!l!+2 & set /a t=!l!+4 & set /a p=!l!+6)
- if !l!==!n! (set nn=!oneline:~3,1!)
- if !l!==!t! (set tt=!oneline!)
- if !l!==!p! (set pp=!oneline:~7! & (echo [ !nn! ] -磁盘- !tt! - !pp!))
- if "!oneline:~0,7!"==" 分区 " (set /a b+=1 & (echo !oneline:~10,1! !oneline:~15,3! !oneline:~33,7!))
- )
- del dp_t.txt
-
- echo.
- echo -----------------------------------------------------
- echo 准备安装 %SEL_OS_NAME%
- echo 采用分区 %CRT_PART_NAME%
- echo -----------------------------------------------------
- echo 请输入目标硬盘 [012...],%ETR%秒后默认硬盘: 0
- choice /c 012 /n>nul /t %ETR% /d 0
- if %errorlevel%==3 (set "SEL_DSK=2")
- if %errorlevel%==2 (set "SEL_DSK=1")
- if %errorlevel%==1 (set "SEL_DSK=0")
- echo 选择硬盘: %SEL_DSK%
-
- :: 检查所有硬盘是否已分区,不包括U盘
- (@echo list volume & @echo exit)|diskpart|find /i "磁盘分区">nul
- if %errorlevel%==0 (set DSK_STAT=1) else (set DSK_STAT=0)
-
- :: 获取硬盘参数
- ((@echo select disk %SEL_DSK% & @echo detail disk & @echo exit)|diskpart)>a.txt
- for /f "skip=10 delims=" %%a in (a.txt) do (set DSK_TYP=%%~a & goto Show)
- :Show
- for /f "tokens=3 skip=12 delims= " %%a in (a.txt) do (set DSK_PROT=%%~a & goto sss1)
- :sss1
- del a.txt
- goto conf
-
- :conf
- cls
- echo.
- echo.
- echo ______________________确认安装方案_____________________
- echo ^| ^|
- echo ^| 目标: %SEL_OS_NAME% ^|
- :: 应答文件: %UNATT%
- echo ^| 目标: %DSK_TYP:~0,15% 磁盘协议:%DSK_PROT% ^|
- echo ^| 分区: %CRT_PART_NAME% ^|
- echo ^| ^|
- if %DSK_STAT%==0 (echo ^| 硬盘未分区 ^|) else (color 4f & echo ^| ★★★ 硬盘已分区-请注意数据安全 ★★★ ^|)
- echo ^| ^|
- echo ^| [ Y ] 确认上述方案 [ X ] 返回 重新选择 ^|
- echo ^|_______________________________________________________^|
- echo.
- echo 请确认方案 [ X / Y ]:
- choice /c xy /n>nul
- if %errorlevel%==2 (goto way)
- if %errorlevel%==1 (goto first)
-
- :way
- :: 跳转到相应方案
- if %CRT_PART%==CPAll goto x%CRT_PART%
- if %CRT_PART%==CP3 goto x%CRT_PART%
- if %CRT_PART%==CP2 goto x%CRT_PART%
-
- :xCPAll
- color 1f
- echo =======================开始硬盘分区=======================
- (
- echo select disk %SEL_DSK%
- echo clean
- echo convert gpt
- :: 创建EFI分区,大小为500M
- echo create partition efi size=500
- echo format quick fs=fat32 label="EFI"
- echo assign letter=z
- :: 创建MSR分区,大小为128M
- echo create partition msr size=128
- echo format quick fs=fat32
- :: 创建主分区
- echo create partition primary
- echo format fs=ntfs quick
- echo assign letter=c
- echo list volume
- echo exit
- )|diskpart
- goto DepOS
-
- :xCP2
- color 1f
- echo =======================开始硬盘分区=======================
- (
- ... 字数限制 省略部分
- )|diskpart
- goto DepOS
-
- :xCP3
- color 1f
- echo =======================开始硬盘分区=======================
- (
- echo select disk %SEL_DSK%
- echo clean
- echo convert gpt
- :: 创建EFI分区,大小为500M
- echo create partition efi size=500
- echo format quick fs=fat32 label="EFI"
- echo assign letter=z
- :: 创建MSR分区,大小为128M
- echo create partition msr size=128
- echo format quick fs=fat32
- :: 创建主分区, 大小为150G,如需修改大小,请直接替换下面size=[ 204806 ]
- echo create partition primary size=204806
- echo format fs=ntfs quick
- echo assign letter=c
- :: 创建第二个主分区, 大小为500G
- echo create partition primary size=512002
- echo format fs=ntfs quick
- echo assign letter=d
- :: 创建第三个主分区, 大小为剩余容量
- echo create partition primary
- echo format fs=ntfs quick
- echo assign letter=e
- echo list volume
- echo exit
- )|diskpart
- goto DepOS
-
-
- :DepOS
- color 0f
- echo 硬盘分区完成
- echo =======================开始部署系统=======================
- ::获取镜像列表dism /get-wiminfo /wimfile:%DIR_CUR%_OS\WIN10_LTSC\sources\install.wim
- imdisk -a -f %SEL_IMG% -m O:
- Dism /Apply-Image /ImageFile:O:\sources\install.wim /Index:%SEL_IDX% /ApplyDir:C:\
- bcdboot c:\Windows /s z: /f uefi /l zh-cn
- md C:\Windows\Panther
- copy %UNATT% C:\Windows\Panther\Unattend.xml /y
-
-
- echo 硬盘分区完成
- echo 系统部署完成
- echo =======================开始拷贝软件=======================
- echo.
- xcopy %DIR_CUR%_DESKTOP\* C:\Users\Public\desktop\必装软件\ /s/e/y
- copy %DIR_CUR%_DESKTOP\PreInst.bat "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\" /y
-
- shutdown -r -t 0
复制代码
|