批處理(.bat/.cmd)調用安裝程式的**靜默/無人值守參數**,配合 start /wait 同步等待安裝完成,實現軟件批量無人值守部署。關鍵在於掌握各類安裝包的靜默參數。
| 封裝工具 | 典型軟件 | 靜默參數 | 備註 |
|---|---|---|---|
| Inno Setup | Chrome, 7-Zip, Notepad++ | /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- | 最常見 |
| NSIS | WinRAR, VLC, uTorrent | /S | 大小寫敏感 |
| MSI (Windows Installer) | Office, Python, Node.js | /qn /norestart 或 /qb! (基本 UI) | 需 msiexec.exe |
| InstallShield | Adobe, 驅動程式 | /s /v"/qn" (需先錄製 .iss 回答文件) | 較複雜 |
| Wise | 舊版軟件 | /s | |
| 自解壓/自定義 | 各類綠色軟件 | /S /quiet /silent 等 | 需試錯或查閱文檔 |
@echo off
setlocal enabledelayedexpansion
:: ========== 配置區 ==========
set "INSTALL_ROOT=%~dp0Software" % 批處理所在目錄下的 Software 文件夾
set "LOG_FILE=%~dp0install.log"
:: 記錄函數
call :Log "=== Installation started at %date% %time% ==="
:: 定義安裝項目: "描述" "安裝文件相對路徑" "參數"
call :Install "7-Zip 23.01" "7z2301-x64.exe" "/S"
call :Install "Google Chrome" "ChromeStandaloneSetup64.exe" "/quiet /norestart"
call :Install "Python 3.11" "python-3.11.5-amd64.exe" "/quiet InstallAllUsers=1 PrependPath=1"
call :Install "Node.js 20" "node-v20.11.0-x64.msi" "/qn /norestart"
call :Install "VS Code" "VSCodeUserSetup-x64-1.85.exe" "/verysilent /suppressmsgboxes /norestart /mergetasks=!runcode"
call :Log "=== All installations completed ==="
echo.
echo Press any key to exit...
pause >nul
exit /b
:: ========== 子程序 ==========
:Install
set "desc=%~1"
set "file=%~2"
set "args=%~3"
set "fullpath=%INSTALL_ROOT%\%file%"
call :Log "Installing: %desc% (%file%)"
if not exist "%fullpath%" (
call :Log " [ERROR] File not found: %fullpath%"
goto :EOF
)
start /wait "" "%fullpath%" %args%
set "ret=%errorlevel%"
if %ret% equ 0 (
call :Log " [OK] Exit code 0"
) else (
call :Log " [FAIL] Exit code %ret%"
)
goto :EOF
:Log
echo [%date% %time%] %~1 >> "%LOG_FILE%"
echo %~1
goto :EOF
start /wait "" "path\to\setup.exe" args"" 防止路徑含空格被誤認為標題。/wait 同步等待進程結束,獲取 %errorlevel%。%~dp0:批處理所在目錄(含尾部反斜槿)。
:: 放在腳本最開頭
net session >nul 2>&1
if %errorlevel% neq 0 (
powershell -command "Start-Process '%~f0' -Verb RunAs"
exit /b
)
:: 在 64 位系統上運行 32 位安裝程式時,系統會將 System32 重定向到 SysWOW64
:: 若需訪問真正的 System32,使用 %windir%\Sysnative
reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Run" /v MyApp /t REG_SZ /d "C:\Program Files\MyApp\app.exe" /f
echo 正在安裝 %desc% ...
:: 可配合進度條工具(如 NirCmd, gnuwin32 pv)或簡單動畫
for /l %%i in (1,1,20) do (
nul
)
echo 完成
/norestart / /NORESTART。shutdown /r /t 30 /c "Installation complete, restarting in 30s"。
@echo off
set "SETUP=SogouPY_14.5.0.9482.exe"
if not exist "%SETUP%" (echo Missing %SETUP% & pause & exit /b)
:: 搜狗靜默參數:/s (全靜默) /q (安靜模式) /n (不創建桌面圖標)
start /wait "" "%SETUP%" /s /q /n
if %errorlevel% equ 0 (
echo Sogou installed successfully.
) else (
echo Installation failed with code %errorlevel%.
)
pause
現代 Windows 包管理器更優雅:
:: Winget (Win10 1809+ 內建)
winget install --id 7zip.7zip --silent --accept-source-agreements
winget install --id Google.Chrome --silent
winget install --id Python.Python.3.11 --silent
:: Chocolatey
choco install 7zip googlechrome python nodejs vscode -y
:: Scoop
scoop install 7zip googlechrome python nodejs vscode
優點:自動解決依賴、版本管理、升級、卸載、跨機器同步配置。批處理適用於**離線環境、特定版本鎖定、嵌入映像構建**場景。
批處理自動安裝的核心是**收集正確的靜默參數** + **start /wait 同步** + **日誌記錄** + **管理員提升**。建立標準化的 Software 目錄結構與模板腳本,新增軟件只需添加一行 call :Install,實現真正的「零接觸部署」。