自动化尾秤安装过程

问题描述 投票:0回答:1

我正在编写一个cmdline批处理脚本来自动执行tailscale的安装过程,因此我需要一种在不提示任何图形窗口的情况下安装它的方法,并且我很难得到这样的错误: screenshot error

我遵循本指南作为参考:

msiexec /L .\install.log /i ./tailscale-setup-1.74.0-amd64.msi TS_NOLAUNCH=yes TS_INSTALLUPDATES=always TS_ALLOWINCOMINGCONNECTIONS=always TS_UNATTENDEDMODE=always

这是我正在编写的完整脚本:

@echo off
setlocal

REM TODO: abort the process if the internet connection is down.

if [%1] == [] goto :usage

goto :install_msys2

:usage
echo USAGE: .\automate.bat "your-auth-key"
echo DESCRIPTION: the script to automate the installation process of msys2, tailscale and openssh
echo also automatically configure the sshd_config file to only accept public key auth method and connect your machine to tailnet.
echo note: you need to manually copy your public key to authorized_keys file

goto :eof

:install_msys2
REM Check if MSYS2 is in the system PATH
where msys2.exe >nul 2>nul
if %errorlevel% NEQ 0 (
    echo [WARN] Failed to search MSYS2 in the PATH.
)

echo [ACTION] Trying to search MSYS2 on the default location: C:\msys64\msys2.exe
REM Check for MSYS2 in default installation directory
if not exist "C:\msys64\msys2.exe" (
    echo [ERROR] can't found MSYS2 on the default location
    echo [ERROR] Exit the script... MSYS2 is not installed.
    REM TODO: download and install the msys2 instead of exit
    REM https://www.msys2.org/docs/installer/
    REM .\msys2-x86_64-latest.exe in --confirm-command --accept-messages --root C:/msys64
    exit /b 1
)

echo [INFO] MSYS2 software is installed on: ...

:install_openssh
REM Check the status of msys2_sshd service
sc query msys2_sshd | findstr /i "STATE" >nul 2>nul

if %errorlevel%==0 (
    echo [INFO] msys2_sshd service is installed.
    sc query msys2_sshd | findstr /i "RUNNING" >nul 2>nul
    if %errorlevel%==0 (
    echo [INFO] msys2_sshd service is already running.
    ) else (
    echo [INFO] msys2_sshd service is not running.
    REM TODO: ensure the service will automatically started on next boot
    net start msys2_sshd
    )
) else (
    echo [ERROR] msys2_sshd service is not installed
    echo [ACTION] starting to install it...
    REM TODO: install openssh according to https://www.msys2.org/wiki/Setting-up-SSHd/
)

:install_tailscale
REM Check if tailscale is in the system PATH
where tailscale.exe >nul 2>nul
if %errorlevel% NEQ 0 (
    echo [WARNING] Failed to search tailscale in the PATH.
)

echo [ACTION] Trying to search tailscale on the default location: C:\Program Files/Tailscale/
if not exist "C:\Program Files\Tailscale\tailscale.exe" (
    echo [WARN] can't found tailscale on the default location
    REM TODO: download and install tailscale silently
    REM download it from https://pkgs.tailscale.com/stable/tailscale-setup-latest.exe
    echo [ACTION] downloading tailscale installer
    powershell -Command "Invoke-WebRequest -Uri https://pkgs.tailscale.com/stable/tailscale-setup-latest.exe -OutFile tailscale-setup-latest.exe

)

echo [INFO] tailscale is installed on: ...

sc query tailscale | findstr /i "STATE" >nul 2>nul

if %errorlevel%==0 (
    echo [INFO] tailscale service is installed.
    sc query msys2_sshd | findstr /i "RUNNING" >nul 2>nul
    if %errorlevel%==0 (
        echo [INFO] tailscale service is already running.
    ) else (
        echo [INFO] tailscale service is not running.
        REM TODO: start tailscale service and ensure the service will automatically started on next boot
    )

    REM TODO: start to connecting the machine to tailnet while authenticate tailscale silently with --unattended argument
) else (
    echo [ERROR] tailscale service is missing, make sure to install the software properly
    exit /b 1
)

该脚本对我来说非常有用,因为我经常需要在虚拟机中重新创建 Windows,但它尚未准备好使用并且仍在进行中:)

我还有一个与该问题的标题无关但仍与 tailscale 有关的问题:

来自tailscale页面

Tailscale MSI 属性(如果设置)存储在

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Tailscale
注册表项中。所有 Tailscale MSI 属性的注册表值类型都是
REG_SZ

如果我安装了 tailscale 并想查看这些属性的当前值,如何使用 cmdline 或 powershell 打印它?

windows batch-file cmd registry tailscale
1个回答
0
投票

主要问题是带有

./tailscale-setup-1.74.0-amd64.msi
的 Microsoft 安装程序文件的规范。 Windows 上的目录分隔符是
\
,而不是 Linux/Mac 上的
/
,如有关命名文件、路径和命名空间的 Microsoft 文档中所述。字符
/
在 Windows 上用于命令行选项。

错误消息给出的信息是在当前工作目录中找不到指定的 MSI 文件。当前工作目录是

C:\mysys64\home\ryuu
,用于
cmd.exe
处理批处理文件。但是,在大多数情况下,执行
msiexec.exe
会导致在本地管理员的提升环境中运行此可执行文件,这可能会导致当前工作目录发生更改,例如: 为什么“以管理员身份运行”发生更改(有时)批处理文件的当前目录? 因此,强烈建议在
msiexec.exe
命令行上指定所有文件及其完全限定的文件名,这意味着驱动器 + 路径 + 名称 + 扩展名包含在
"
中,以便独立于当前工作目录。


可以在命令提示符窗口中执行:

%SystemRoot%\System32\reg.exe QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Tailscale

输出

Tailscale
的组策略设置。如果存在注册表值低于指定注册表项的子项,则可能需要附加
/S
。在命令提示符窗口
reg /?
和下一个
reg query /?
中运行,并阅读此 Windows 命令的输出使用帮助。

如果该命令也在批处理文件中使用,请考虑 Microsoft 文档页面WOW64 实现详细信息文件系统重定向器受 WOW64 影响的注册表项上的信息。该批处理文件也可以在 64 位 Windows 上通过 32 位

%SystemRoot%\SysWOW64\cmd.exe
而不是 64 位
%SystemRoot%\System32\cmd.exe
进行处理。


改进批处理代码的更多提示:

  1. 第二行应从

    setlocal
    修改为
    setlocal EnableExtensions DisableDelayedExpansion
    。为此,请阅读此答案中的章节问题 6:批处理文件取决于外部定义的环境

  2. 条件

    if [%1] == [] goto :usage
    是MS-DOS和Windows 95/98/ME的
    COMMAND.COM
    的语法,不支持像
    %~1
    这样的参数字符串修饰符。这些操作系统肯定不再用于运行此批处理文件。应该使用 Windows 命令处理器
    cmd.exe
    语法:
    if "%~1" == "" goto :usage

  3. 所有具有已知路径的可执行文件都应使用完全限定的文件名来引用,以使批处理文件尽可能独立于环境变量

    PATHEXT
    PATH
    ,这意味着更安全,处理速度也更快文件系统访问较少。应该使用
    %SystemRoot%\System32\where.exe
    而不是仅
    where
    %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
    而不是仅
    powershell
    %SystemRoot%\System32\sc.exe
    而不是仅
    sc
    %SystemRoot%\System32\findstr.exe
    而不是仅
    findstr
    %SystemRoot%\System32\msiexec.exe
    而不是仅
    msiexec

  4. 打开命令提示符窗口,运行

    if /?
    并阅读输出用法帮助,该帮助已在第一页上解释如何评估先前运行的命令或可执行文件的退出代码。无法读取批处理文件中使用的有关
    if %errorlevel% NEQ 0
    if %errorlevel%==0
    的任何信息。原因是这些替代退出代码评估的处理速度较慢,并且不能在以
    (
    开头并以匹配的
    )
    结尾的命令块内工作,如批处理文件中所使用的那样。
    if %errorlevel% NEQ 0
    应替换为
    if errorlevel 1
    ,并且
    if %errorlevel%==0
    必须替换为
    if not errorlevel 1
    。有关详细说明,请参阅CMD 中的动态变量和环境变量之间的差异中的访问动态变量的值一章。

Stack Overflow 上的其他相关问题和解答:

© www.soinside.com 2019 - 2024. All rights reserved.