我正在尝试编写一个简短的Windows脚本,它将使用chocolatey安装一些不同的软件包,但它也将在安装之间重新启动计算机,然后继续使用该脚本。以下是我到目前为止:
@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
)
该脚本运行正常,但第三个脚本似乎失败并且未成功完成。看起来没有任何反应。我到底错过了什么?
谢谢!
您正在添加到注册表的RunOnce部分。你想要它运行多少次? :)
看起来你缺少reg添加调用将其添加回注册表中:两个OR你应该使用Run部分而不是RunOnce
@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
for goto %current.txt%
,这将是未定义的。
该脚本需要重复使用注册表runonce
密钥,以便可以在标签中使用它。
不需要文本文件,因为您可以使用脚本参数传递标签名称以与goto
一起使用。第一个脚本执行没有参数,因此将运行标签:one
。