本文目录导读:

Windows 平台
MSI 安装包
:: 静默安装MSI msiexec /i "软件.msi" /qn /norestart :: 带参数安装 msiexec /i "软件.msi" /qn INSTALLDIR="C:\Program Files\Software" LICENSE_KEY="xxxxx"
EXE 安装包(常见参数)
:: 静默安装 "installer.exe" /S /silent /quiet :: 常用参数示例 "installer.exe" /quiet /norestart "installer.exe" /silent /nogui
PowerShell 脚本
# 下载并静默安装
$installer = "https://example.com/software.exe"
Invoke-WebRequest -Uri $installer -OutFile "$env:TEMP\install.exe"
Start-Process -FilePath "$env:TEMP\install.exe" -ArgumentList "/S" -Wait
# 检查安装是否成功
if (Test-Path "C:\Program Files\Software") {
Write-Host "安装成功"
}
Linux 平台
Debian/Ubuntu (apt)
# 完全静默安装 DEBIAN_FRONTEND=noninteractive apt-get install -y <package> # 或者 apt-get install -y --no-install-recommends <package>
RHEL/CentOS (yum/dnf)
# RPM安装 rpm -ivh package.rpm # yum静默安装 yum install -y package # dnf静默安装 dnf install -y package
通用脚本示例
#!/bin/bash
# 自动检测包管理器
if [ -f /etc/debian_version ]; then
DEBIAN_FRONTEND=noninteractive apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y nginx
elif [ -f /etc/redhat-release ]; then
yum install -y nginx
fi
macOS 平台
# 通过brew静默安装 brew install --quiet <formula> # DMG安装 hdiutil attach software.dmg cp -R "/Volumes/Software/Software.app" /Applications/ hdiutil detach "/Volumes/Software" # PKG安装 sudo installer -pkg software.pkg -target /
跨平台脚本示例
远程批量部署
# PowerShell远程部署
$computers = @("PC01", "PC02", "PC03")
$software = "\\server\share\software.msi"
foreach ($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock {
param($sw)
msiexec /i $sw /qn
} -ArgumentList $software
}
使用 Chocolatey(Windows包管理器)
# 安装Chocolatey后 choco install googlechrome -y --silent choco install notepadplusplus -y --silent # 批量安装 choco install git vscode putty -y --silent
使用 Winget
# Windows 10/11 自带 winget install --silent --accept-package-agreements Google.Chrome winget install --id Microsoft.VisualStudioCode --silent
自动化脚本模板
完整的静默安装脚本示例
# silent_install.ps1
param(
[string]$SoftwareUrl = "https://example.com/software.exe",
[string]$InstallArgs = "/S"
)
$LogFile = "C:\Logs\install.log"
function Write-Log {
param([string]$Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Out-File $LogFile -Append
}
try {
Write-Log "开始下载软件..."
$tempFile = "$env:TEMP\installer.exe"
Invoke-WebRequest -Uri $SoftwareUrl -OutFile $tempFile
Write-Log "开始静默安装..."
$process = Start-Process -FilePath $tempFile -ArgumentList $InstallArgs -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Log "安装成功"
} else {
Write-Log "安装失败,退出代码: $($process.ExitCode)"
}
# 清理临时文件
Remove-Item $tempFile -Force
} catch {
Write-Log "错误: $($_.Exception.Message)"
exit 1
}
最佳实践建议
检查安装结果
# Linux检查是否安装成功
dpkg -l | grep package_name
rpm -qa | grep package_name
# Windows检查注册表或安装目录
if (Test-Path "HKLM:\Software\SoftwareVendor") {
Write-Host "已安装"
}
错误处理
#!/bin/bash
set -e # 遇到错误立即退出
INSTALL_LOG="/var/log/install.log"
function log() {
echo "$(date): $1" >> $INSTALL_LOG
}
# 尝试安装,失败则记录日志
if ! apt-get install -y nginx >> $INSTALL_LOG 2>&1; then
log "安装nginx失败"
exit 1
fi
需要我针对特定的软件或场景提供更详细的脚本吗?