如何用脚本快速部署批量软件

wen 实用脚本 2

本文目录导读:

如何用脚本快速部署批量软件

  1. Windows 批量部署脚本
  2. Linux 批量部署脚本
  3. macOS 批量部署脚本
  4. 跨平台批量部署工具
  5. 使用技巧

Windows 批量部署脚本

PowerShell 脚本

# 批量安装软件脚本
param(
    [string]$SoftwareList = "software.txt",
    [string]$InstallDir = "C:\Software",
    [switch]$Silent
)
# 读取软件列表
$softwares = Get-Content $SoftwareList
foreach ($software in $softwares) {
    $installer = Join-Path $InstallDir $software
    if (Test-Path $installer) {
        Write-Host "正在安装: $software" -ForegroundColor Green
        # 根据文件类型选择安装方式
        switch -Wildcard ($software) {
            "*.msi" { 
                if ($Silent) {
                    Start-Process msiexec.exe -ArgumentList "/i `"$installer`" /qn" -Wait
                } else {
                    Start-Process msiexec.exe -ArgumentList "/i `"$installer`"" -Wait
                }
            }
            "*.exe" { 
                if ($Silent) {
                    Start-Process $installer -ArgumentList "/S" -Wait
                } else {
                    Start-Process $installer -Wait
                }
            }
            default { 
                Write-Host "不支持的文件类型: $software" -ForegroundColor Red
            }
        }
    } else {
        Write-Host "文件不存在: $installer" -ForegroundColor Red
    }
}

批处理脚本

@echo off
setlocal enabledelayedexpansion
:: 设置软件目录
set "SOFTWARE_DIR=C:\Software"
set "LOG_FILE=install.log"
:: 清空日志
echo. > %LOG_FILE%
:: 遍历软件文件
for %%f in ("%SOFTWARE_DIR%\*.msi" "%SOFTWARE_DIR%\*.exe") do (
    echo 正在安装: %%~nxf
    echo %date% %time% - 开始安装: %%~nxf >> %LOG_FILE%
    if /i "%%~xf"==".msi" (
        msiexec /i "%%f" /qn /norestart
    ) else if /i "%%~xf"==".exe" (
        "%%f" /S /v"/qn"
    )
    if !errorlevel! equ 0 (
        echo 安装成功: %%~nxf
        echo %date% %time% - 成功: %%~nxf >> %LOG_FILE%
    ) else (
        echo 安装失败: %%~nxf
        echo %date% %time% - 失败: %%~nxf >> %LOG_FILE%
    )
)
echo 批量安装完成
pause

Linux 批量部署脚本

Bash 脚本

#!/bin/bash
# 批量安装软件脚本
SOFTWARE_LIST="software.txt"
LOG_FILE="/var/log/bulk_install.log"
INSTALL_DIR="/opt/software"
# 检查是否为 root
if [ "$EUID" -ne 0 ]; then 
    echo "请用 root 权限运行"
    exit 1
fi
# 创建日志
echo "======= 批量安装开始 $(date) =======" > $LOG_FILE
# 读取软件列表
while IFS= read -r software; do
    if [ -z "$software" ]; then
        continue
    fi
    echo "安装: $software" | tee -a $LOG_FILE
    case $software in
        *.deb)
            dpkg -i "$INSTALL_DIR/$software" >> $LOG_FILE 2>&1
            if [ $? -eq 0 ]; then
                echo "成功: $software" | tee -a $LOG_FILE
            else
                echo "失败: $software" | tee -a $LOG_FILE
            fi
            ;;
        *.rpm)
            rpm -ivh "$INSTALL_DIR/$software" >> $LOG_FILE 2>&1
            if [ $? -eq 0 ]; then
                echo "成功: $software" | tee -a $LOG_FILE
            else
                echo "失败: $software" | tee -a $LOG_FILE
            fi
            ;;
        apt|yum)
            # 从包管理器安装
            if command -v apt-get &> /dev/null; then
                apt-get install -y $software >> $LOG_FILE 2>&1
            elif command -v yum &> /dev/null; then
                yum install -y $software >> $LOG_FILE 2>&1
            fi
            ;;
        *)
            echo "不支持的类型: $software" | tee -a $LOG_FILE
            ;;
    esac
done < "$SOFTWARE_LIST"
echo "======= 批量安装完成 $(date) =======" >> $LOG_FILE

macOS 批量部署脚本

#!/bin/bash
# macOS 批量安装脚本
SOFTWARE_LIST="software.txt"
LOG_FILE="$HOME/Desktop/install_log.txt"
# 创建日志
echo "批量安装日志 - $(date)" > $LOG_FILE
# 检查 Homebrew
if ! command -v brew &> /dev/null; then
    echo "安装 Homebrew..." | tee -a $LOG_FILE
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# 读取软件列表
while IFS= read -r software; do
    if [ -z "$software" ]; then
        continue
    fi
    echo "安装: $software" | tee -a $LOG_FILE
    case $software in
        *.dmg)
            # 挂载 DMG 并安装
            hdiutil attach "$software"
            cp -R "/Volumes/$(basename $software .dmg)"/*.app /Applications/
            hdiutil detach "/Volumes/$(basename $software .dmg)"
            echo "成功: $software" | tee -a $LOG_FILE
            ;;
        *.pkg)
            installer -pkg "$software" -target /
            echo "成功: $software" | tee -a $LOG_FILE
            ;;
        *.app)
            cp -R "$software" /Applications/
            echo "成功: $software" | tee -a $LOG_FILE
            ;;
        *)
            # 尝试 Homebrew
            brew install "$software" >> $LOG_FILE 2>&1
            if [ $? -eq 0 ]; then
                echo "成功: $software" | tee -a $LOG_FILE
            else
                brew cask install "$software" >> $LOG_FILE 2>&1
                if [ $? -eq 0 ]; then
                    echo "成功: $software" | tee -a $LOG_FILE
                else
                    echo "失败: $software" | tee -a $LOG_FILE
                fi
            fi
            ;;
    esac
done < "$SOFTWARE_LIST"

跨平台批量部署工具

Chocolatey (Windows)

# 软件列表文件
choco install -y --ignore-checksums --force 7zip notepadplusplus firefox chrome vlc
# 从文件批量安装
Get-Content software.txt | ForEach-Object {
    choco install -y $_
}

Ansible 批量部署

---
- name: 批量安装软件
  hosts: all
  become: yes
  tasks:
    - name: 安装 Windows 软件
      win_chocolatey:
        name: "{{ item }}"
        state: present
      loop:
        - 7zip
        - notepadplusplus
        - firefox
    - name: 安装 Linux 软件
      package:
        name: "{{ item }}"
        state: present
      loop:
        - vim
        - htop
        - git

使用技巧

  1. 准备软件列表文件 (software.txt):

    chrome_installer.exe
    vlc.msi
    teamviewer.msi
  2. 设置静默安装参数(通用参数):

    • /S/silent - 静默安装
    • /qn - 无人值守 MSI 安装
    • --quiet - 安静模式
  3. 选择合适的部署方式:

    • 小规模 (10-50台): 本地脚本
    • 中规模 (50-500台): SCCM/PDQ
    • 大规模 (500+台): 组策略/Ansible
  4. 测试环境验证: 先在测试环境运行,确认所有软件能够正确安装。

根据您的具体需求(操作系统、网络环境、软件类型),选择最适合的部署方案。

抱歉,评论功能暂时关闭!