Board logo

标题: [原创代码] 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:盘。
批处理代码如下:
  1. if ($true){}# == ($true){}# goto ___yiwuyun
  2. <#BeginBatOperation#
  3. :___yiwuyun
  4. @echo off&setlocal EnableDelayedExpansion&cls
  5. if "%~1"=="true" (
  6. (echo $yiwuyun_fileName="%~f0"&echo $BoolAdmin=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  7. call :MainBatOperation
  8. ) else if "%~1"=="delboot" (
  9. (echo $yiwuyun_fileName="%~f0"&echo $Del=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  10. ) else if "%~1"=="deltrue" (
  11. (echo $yiwuyun_fileName="%~f0"&echo $Del=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  12. call :MainBatOperation delboot
  13. ) else (
  14. (echo $yiwuyun_fileName="%~f0"&echo $BoolAdmin=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  15. )
  16. endlocal
  17. exit/b 0
  18. :MainBatOperation
  19. rem 初始化变量
  20. set drive=C:
  21. set "ramdiskwim=ramdisk=[!drive!]\wuyun\bootimg\win8cmdboot.wim"
  22. set "ramdisksdipath=\wuyun\boot\boot.sdi"
  23. set "part=partition=!drive!"
  24. set "delboottxt=!drive!\wuyun\boot\delboot.txt"
  25. set "bootsdi=!drive!\wuyun\boot\boot.sdi"
  26. set "bootwim=!drive!\wuyun\bootimg\win8cmdboot.wim"
  27. set  description="Boot from CMD"
  28. rem 判断所需的文件是否存在,不存在就退出
  29. call :FileIsExist
  30. if not "%errorlevel%"=="0" (
  31.   echo File Not Exist!
  32.   goto end
  33. )
  34. rem 如果第一个参数是delboot 则删除由本程序添加的所有引导项,如果要删除最近的。必须手动修改delboot.txt,只保留最后两项。
  35. call :DelBoot %1
  36. if "%errorlevel%"=="1" (
  37.   echo File Not Exist!
  38.   goto end
  39. ) else (
  40.   if "%errorlevel%"=="2" (
  41.     echo Boot Item Deleted!
  42.     goto end
  43.   )
  44. )
  45. rem 新建引导加载项 加载项类型为ramdisk
  46. call :NewRamDisk
  47. rem 获取原来的引导项顺序
  48. call :DisplayOrder
  49. rem 把新建的引导项添加到末尾
  50. call :AddToTail
  51. endlocal
  52. :end
  53. pause
  54. exit /b 0
  55. :FileIsExist
  56. setlocal
  57. if not exist !bootsdi! exit /b 1
  58. if not exist !bootwim! exit /b 1
  59. endlocal
  60. exit /b 0
  61. :DelBoot
  62. setlocal
  63. if "%1"=="delboot" (
  64.   if not exist !delboottxt! exit /b 1
  65.   for /f "eol=; tokens=1,2,3 delims={}" %%a in (!delboottxt!) do (
  66.     if not "%%b"=="" set "DelGuid={%%b}"
  67.     >nul bcdedit /delete !DelGuid!
  68.     echo Boot Item Deleted: !DelGuid!
  69.   )
  70.   del !delboottxt!
  71.   exit /b 2
  72. )
  73. endlocal
  74. exit /b 0
  75. :NewRamDisk
  76. setlocal
  77. for /f "eol=; tokens=1,2,3 delims={}" %%a in ('bcdedit /create /application osloader') do (
  78.   if not "%%b"=="" set "OSLoaderGuid={%%b}"
  79. )
  80. for /f "eol=; tokens=1,2,3 delims={}" %%a in ('bcdedit /create /device') do (
  81.   if not "%%b"=="" set "RamGuid={%%b}"
  82. )
  83. >nul bcdedit /set !RamGuid! ramdisksdidevice !part!
  84. >nul bcdedit /set !RamGuid! ramdisksdipath !ramdisksdipath!
  85. >nul bcdedit /set !OSLoaderGuid! device !ramdiskwim!,!RamGuid!
  86. >nul bcdedit /set !OSLoaderGuid! osdevice !ramdiskwim!,!RamGuid!
  87. >nul bcdedit /set !OSLoaderGuid! path \Windows\system32\boot\winload.efi
  88. >nul bcdedit /set !OSLoaderGuid! description !description!
  89. >nul bcdedit /set !OSLoaderGuid! systemroot \windows
  90. >nul bcdedit /set !OSLoaderGuid! winpe Yes
  91. echo RamGuid Boot Item Created :!RamGuid!
  92. echo OSLoaderGuid Boot Item Created :!OSLoaderGuid!
  93. >>!delboottxt!  echo RamGuid:!RamGuid!
  94. >>!delboottxt!  echo OSLoaderGuid:!OSLoaderGuid!
  95. endlocal&set OSLoaderGuid=%OSLoaderGuid%
  96. exit /b 0
  97. :DisplayOrder
  98. setlocal
  99. set displayOrder=
  100. for /f "eol=- tokens=1,2 delims= " %%a in ('bcdedit /enum {bootmgr}') do (
  101.   if "%%a"=="displayorder" (
  102.      set displayOrder=%%b
  103.   )
  104. )
  105. for /f "eol=- tokens=1,2 delims= " %%a in ('bcdedit /enum {bootmgr}') do (
  106.   if "%%b"=="" (
  107.      if "!displayOrder!"=="" (
  108.        set "displayOrder=%%a"
  109.      ) else (
  110.        set "displayOrder=!displayOrder! %%a"
  111.      )
  112.   )
  113. )
  114. endlocal&set displayOrder=%displayOrder%
  115. exit /b 0
  116. :AddToTail
  117. setlocal
  118. >nul bcdedit /set {bootmgr} displayorder !displayOrder! !OSLoaderGuid!
  119. >nul bcdedit /set {bootmgr} timeout 5
  120. echo Boot Item Created!
  121. endlocal
  122. exit /b 0
  123. #EndBatOperation#>
  124. <#StartPowerShell#>
  125. Function RunAsAdmin{
  126. $AdminPrivilege=New-Object -ComObject "Shell.Application";
  127. $AdminPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName true",0,"runas",1);
  128. }
  129. Function RunAsAdminDel{
  130. $AdminPrivilege=New-Object -ComObject "Shell.Application";
  131. $AdminPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName deltrue",0,"runas",1);
  132. }
  133. if($BoolAdmin){
  134.   RunAsAdmin;
  135. }elseif($Del){
  136.   RunAsAdminDel;
  137. }else{
  138. }
  139. <#EndPowerShell#>
复制代码

作者: yiwuyun    时间: 2019-7-5 09:56

本帖最后由 yiwuyun 于 2019-7-5 10:09 编辑

复制代码不一定能用。其中的回车行不要缺失!我刚在单位下载了试了,好像不行。一定要把回车行都加上。可以原文复制,也行。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2