首页 > 解决方案 > 带有 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
)

该脚本运行正常,但第三个似乎失败且未成功完成。看起来什么都没有发生。我到底错过了什么?

谢谢!

标签: shellbatch-filechocolatey

解决方案


@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.txtgoto %current.txt%

该脚本需要重复使用注册表 runonce项,以便可以在标签中使用。

不需要文本文件,因为您可以使用脚本参数传递标签名称以用于goto. 第一个脚本执行将没有参数,因此:one将运行标签。


推荐阅读