如何写一个脚本批量安装应用

wen 实用脚本 3

本文目录导读:

如何写一个脚本批量安装应用

  1. Windows 环境 (.bat 或 PowerShell)
  2. macOS 环境 (.sh)
  3. Linux 环境 (.sh)
  4. 跨平台 Python 脚本
  5. 使用建议
  6. 执行脚本

我来提供几种批量安装应用的方法,适用于不同平台:

Windows 环境 (.bat 或 PowerShell)

使用 Chocolatey 包管理器(推荐)

# 首先安装 Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# 批量安装应用列表
$apps = @(
    "googlechrome",
    "firefox",
    "notepadplusplus",
    "7zip",
    "vscode",
    "git",
    "nodejs",
    "python"
)
foreach ($app in $apps) {
    Write-Host "正在安装: $app"
    choco install $app -y
    Write-Host "$app 安装完成" -ForegroundColor Green
}

使用 winget (Windows 10/11)

# 批量安装脚本
$apps = @(
    "Microsoft.VisualStudioCode",
    "Google.Chrome",
    "Mozilla.Firefox",
    "7zip.7zip",
    "Git.Git",
    "VideoLAN.VLC"
)
foreach ($app in $apps) {
    Write-Host "正在安装: $app"
    winget install --id $app --silent --accept-package-agreements
    Write-Host "$app 安装完成" -ForegroundColor Green
}

macOS 环境 (.sh)

使用 Homebrew

#!/bin/bash
# 安装 Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 应用列表
apps=(
    "google-chrome"
    "firefox"
    "visual-studio-code"
    "iterm2"
    "vlc"
    "notion"
    "postman"
    "docker"
)
# 批量安装
for app in "${apps[@]}"; do
    echo "正在安装: $app"
    brew install --cask "$app"
    echo "$app 安装完成"
done
# 安装命令行工具
cli_tools=(
    "git"
    "node"
    "python@3.9"
    "wget"
    "htop"
)
for tool in "${cli_tools[@]}"; do
    echo "正在安装: $tool"
    brew install "$tool"
done

Linux 环境 (.sh)

Ubuntu/Debian

#!/bin/bash
# 更新包列表
sudo apt update
# 应用列表
apps=(
    "google-chrome-stable"
    "firefox"
    "code"
    "vlc"
    "gimp"
    "git"
    "curl"
    "wget"
    "python3-pip"
    "nodejs"
    "npm"
)
# 批量安装
for app in "${apps[@]}"; do
    echo "正在安装: $app"
    sudo apt install -y "$app"
    echo "$app 安装完成"
done
# 安装 snap 应用
snap_apps=(
    "spotify"
    "slack"
    "postman"
)
for app in "${snap_apps[@]}"; do
    echo "正在安装: $app"
    sudo snap install "$app"
done

跨平台 Python 脚本

创建一个 install_apps.py 文件:

#!/usr/bin/env python3
import platform
import subprocess
import sys
def install_windows():
    apps = [
        "googlechrome",
        "firefox",
        "notepadplusplus",
        "7zip",
        "vscode",
        "git"
    ]
    for app in apps:
        print(f"正在安装: {app}")
        subprocess.run(["choco", "install", app, "-y"], check=True)
        print(f"{app} 安装完成")
def install_macos():
    apps = [
        "google-chrome",
        "firefox",
        "visual-studio-code",
        "iterm2",
        "vlc"
    ]
    for app in apps:
        print(f"正在安装: {app}")
        subprocess.run(["brew", "install", "--cask", app], check=True)
        print(f"{app} 安装完成")
def install_linux():
    apps = [
        "firefox",
        "vlc",
        "git",
        "curl",
        "python3-pip"
    ]
    subprocess.run(["sudo", "apt", "update"], check=True)
    for app in apps:
        print(f"正在安装: {app}")
        subprocess.run(["sudo", "apt", "install", "-y", app], check=True)
        print(f"{app} 安装完成")
def main():
    system = platform.system()
    print(f"检测到系统: {system}")
    print("开始批量安装应用...")
    if system == "Windows":
        install_windows()
    elif system == "Darwin":  # macOS
        install_macos()
    elif system == "Linux":
        install_linux()
    else:
        print(f"不支持的操作系统: {system}")
        sys.exit(1)
    print("所有应用安装完成!")
if __name__ == "__main__":
    main()

使用建议

  1. 先测试:在少量机器上测试脚本
  2. 添加错误处理:避免一个失败导致全部中断
  3. 使用配置文件:将应用列表放在外部配置文件中
  4. 添加日志:记录安装成功/失败的应用
  5. 权限处理:确保有足够的权限执行安装

执行脚本

# Windows PowerShell
powershell -ExecutionPolicy Bypass -File install_apps.ps1
# macOS/Linux
chmod +x install_apps.sh
./install_apps.sh
# Python 跨平台
python3 install_apps.py

选择最适合你需求的方案,建议先在测试环境中验证。

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