标题: [原创代码] powershell+批处理安装WINPE到C盘 [打印本页]
作者: yiwuyun 时间: 2019-7-4 21:10 标题: powershell+批处理安装WINPE到C盘
最近想在硬盘上安装winpe,结果U盘上的原有批处理不能使用,捣鼓了一下,发现是boot.sdi的问题,U盘上用的boot.sdi不能用到硬盘上来。设置也有所不同。无事就又写了一个批处理的安装程序,也好记下来以后用。
目录结构如下,都是可自定义的。
c:\wuyun\boot\boot.sdi //boot.sdi文件,一般在C:盘的recovery文件夹下可以找到,是隐藏了的。
c:\wuyun\bootimg\win8cmdboot.wim //winpe,每人用的不同,我直接用的boot.wim改名的。是cmd下用的。网上有很多图形界面的,改个名就可直接用。
c:\wuyun\boot\delboot.txt //这个是批处理自建的。用于删除引导项,还原原来的设置。
批处理如果带有参数 delboot 就表示删除引导项 。比如如果批处理名为bd.bat ,则bd.bat delboot就表示删除引导项。双击bd.bat就表示安装WINPE到C:盘。
批处理代码如下:- if ($true){}# == ($true){}# goto ___yiwuyun
- <#BeginBatOperation#
- :___yiwuyun
- @echo off&setlocal EnableDelayedExpansion&cls
- if "%~1"=="true" (
- (echo $yiwuyun_fileName="%~f0"&echo $BoolAdmin=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- call :MainBatOperation
- ) else if "%~1"=="delboot" (
- (echo $yiwuyun_fileName="%~f0"&echo $Del=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- ) else if "%~1"=="deltrue" (
- (echo $yiwuyun_fileName="%~f0"&echo $Del=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- call :MainBatOperation delboot
- ) else (
- (echo $yiwuyun_fileName="%~f0"&echo $BoolAdmin=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- )
- endlocal
- exit/b 0
-
- :MainBatOperation
- rem 初始化变量
- set drive=C:
- set "ramdiskwim=ramdisk=[!drive!]\wuyun\bootimg\win8cmdboot.wim"
- set "ramdisksdipath=\wuyun\boot\boot.sdi"
- set "part=partition=!drive!"
- set "delboottxt=!drive!\wuyun\boot\delboot.txt"
- set "bootsdi=!drive!\wuyun\boot\boot.sdi"
- set "bootwim=!drive!\wuyun\bootimg\win8cmdboot.wim"
- set description="Boot from CMD"
-
- rem 判断所需的文件是否存在,不存在就退出
- call :FileIsExist
- if not "%errorlevel%"=="0" (
- echo File Not Exist!
- goto end
- )
-
- rem 如果第一个参数是delboot 则删除由本程序添加的所有引导项,如果要删除最近的。必须手动修改delboot.txt,只保留最后两项。
- call :DelBoot %1
- if "%errorlevel%"=="1" (
- echo File Not Exist!
- goto end
- ) else (
- if "%errorlevel%"=="2" (
- echo Boot Item Deleted!
- goto end
- )
- )
-
-
- rem 新建引导加载项 加载项类型为ramdisk
- call :NewRamDisk
-
-
- rem 获取原来的引导项顺序
- call :DisplayOrder
-
- rem 把新建的引导项添加到末尾
- call :AddToTail
-
- endlocal
- :end
- pause
- exit /b 0
-
-
- :FileIsExist
- setlocal
- if not exist !bootsdi! exit /b 1
- if not exist !bootwim! exit /b 1
- endlocal
- exit /b 0
-
- :DelBoot
- setlocal
- if "%1"=="delboot" (
- if not exist !delboottxt! exit /b 1
- for /f "eol=; tokens=1,2,3 delims={}" %%a in (!delboottxt!) do (
- if not "%%b"=="" set "DelGuid={%%b}"
- >nul bcdedit /delete !DelGuid!
- echo Boot Item Deleted: !DelGuid!
- )
- del !delboottxt!
- exit /b 2
- )
- endlocal
- exit /b 0
-
-
- :NewRamDisk
- setlocal
- for /f "eol=; tokens=1,2,3 delims={}" %%a in ('bcdedit /create /application osloader') do (
- if not "%%b"=="" set "OSLoaderGuid={%%b}"
- )
-
- for /f "eol=; tokens=1,2,3 delims={}" %%a in ('bcdedit /create /device') do (
- if not "%%b"=="" set "RamGuid={%%b}"
- )
-
- >nul bcdedit /set !RamGuid! ramdisksdidevice !part!
- >nul bcdedit /set !RamGuid! ramdisksdipath !ramdisksdipath!
-
- >nul bcdedit /set !OSLoaderGuid! device !ramdiskwim!,!RamGuid!
- >nul bcdedit /set !OSLoaderGuid! osdevice !ramdiskwim!,!RamGuid!
- >nul bcdedit /set !OSLoaderGuid! path \Windows\system32\boot\winload.efi
- >nul bcdedit /set !OSLoaderGuid! description !description!
- >nul bcdedit /set !OSLoaderGuid! systemroot \windows
- >nul bcdedit /set !OSLoaderGuid! winpe Yes
-
- echo RamGuid Boot Item Created :!RamGuid!
- echo OSLoaderGuid Boot Item Created :!OSLoaderGuid!
-
- >>!delboottxt! echo RamGuid:!RamGuid!
- >>!delboottxt! echo OSLoaderGuid:!OSLoaderGuid!
- endlocal&set OSLoaderGuid=%OSLoaderGuid%
- exit /b 0
-
- :DisplayOrder
- setlocal
- set displayOrder=
- for /f "eol=- tokens=1,2 delims= " %%a in ('bcdedit /enum {bootmgr}') do (
- if "%%a"=="displayorder" (
- set displayOrder=%%b
- )
- )
-
- for /f "eol=- tokens=1,2 delims= " %%a in ('bcdedit /enum {bootmgr}') do (
- if "%%b"=="" (
- if "!displayOrder!"=="" (
- set "displayOrder=%%a"
- ) else (
- set "displayOrder=!displayOrder! %%a"
- )
- )
- )
- endlocal&set displayOrder=%displayOrder%
- exit /b 0
-
-
- :AddToTail
- setlocal
- >nul bcdedit /set {bootmgr} displayorder !displayOrder! !OSLoaderGuid!
- >nul bcdedit /set {bootmgr} timeout 5
- echo Boot Item Created!
- endlocal
- exit /b 0
-
- #EndBatOperation#>
-
-
- <#StartPowerShell#>
-
- Function RunAsAdmin{
- $AdminPrivilege=New-Object -ComObject "Shell.Application";
- $AdminPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName true",0,"runas",1);
- }
-
- Function RunAsAdminDel{
- $AdminPrivilege=New-Object -ComObject "Shell.Application";
- $AdminPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName deltrue",0,"runas",1);
- }
-
- if($BoolAdmin){
- RunAsAdmin;
- }elseif($Del){
- RunAsAdminDel;
- }else{
- }
-
- <#EndPowerShell#>
复制代码
作者: yiwuyun 时间: 2019-7-5 09:56
本帖最后由 yiwuyun 于 2019-7-5 10:09 编辑
复制代码不一定能用。其中的回车行不要缺失!我刚在单位下载了试了,好像不行。一定要把回车行都加上。可以原文复制,也行。
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |