shell - 带有 Chocolatey 安装和在步骤之间重新启动的 Windows 脚本
问题描述
我正在尝试编写一个简短的 windows 脚本,它将使用巧克力安装几个不同的包,但它也会在安装之间重新启动计算机,然后继续执行脚本。以下是我到目前为止的内容:
@echo off
call :Resume
goto %current.txt%
goto :eof
:one
::Add script to Run key
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
Choco install -y IOLibs
pause
shutdown -r -t 0
goto :eof
:two
echo three >%~dp0current.txt
echo -- Section two --
Choco install -y MSCDriver
pause
shutdown -r -t 0
goto :eof
:three
::Remove script from Run key
del c:\temp\current.txt
reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v %~n0 /f
echo -- Section three --
Choco install -y HPPDriver
pause
goto :eof
:resume
if exist %~dp0current.txt (
set /p current=<%~dp0current.txt
) else (
set current=one
)
该脚本运行正常,但第三个似乎失败且未成功完成。看起来什么都没有发生。我到底错过了什么?
谢谢!
解决方案
@echo off
setlocal
if not "%~1" == "" goto %~1
:one
echo -- Section one --
Choco install -y IOLibs
pause
call :runonce "%~f0" two
goto :eof
:two
echo -- Section two --
Choco install -y MSCDriver
pause
call :runonce "%~f0" three
goto :eof
:three
echo -- Section three --
Choco install -y HPPDriver
pause
goto :eof
:runonce
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v "%~n1" /d "\"%~dpnx1\" \"%~2\"" /f
shutdown -r -t 0
您的脚本设置变量名,并且
current
您使用未定义的变量名。current.txt
goto %current.txt%
该脚本需要重复使用注册表
runonce
项,以便可以在标签中使用。
不需要文本文件,因为您可以使用脚本参数传递标签名称以用于goto
. 第一个脚本执行将没有参数,因此:one
将运行标签。
推荐阅读
- javascript - WhatsApp API 不会打开 URL 中链接的联系人
- angular - Angular Material mat-ripple 不起作用
- javascript - RegEx 用于验证整数或带两位小数的浮点数
- java - JavaFX折线图改变颜色形状
- javascript - 如何将 jquery 中的输出格式化为 div?
- vba - 使用参数插入新记录
- java - 如何将此语句转换为三元?
- java - 如何按添加顺序绘制 X、Y 坐标?
- elasticsearch - Elasticsearch - 使用分析器创建索引时出现非法参数异常
- ruby - How to safely encode cidr notation in url?