如何写一个脚本自动静默卸载

wen 实用脚本 2

本文目录导读:

如何写一个脚本自动静默卸载

  1. Windows 环境
  2. Linux 环境
  3. macOS 环境
  4. 高级脚本示例
  5. 使用建议
  6. 安全注意事项

Windows 环境

使用 msiexec 卸载 MSI 程序

@echo off
:: 静默卸载指定程序
msiexec /x {产品GUID} /quiet /norestart
:: 示例:卸载 7-Zip
msiexec /x {23170F69-40C1-2702-0920-000001000000} /quiet /norestart

使用 wmic 查找并卸载

@echo off
:: 查找程序并卸载
wmic product where "name='程序名称'" call uninstall /nointeractive
:: 例如卸载 Adobe Reader
wmic product where "name like '%%Adobe Reader%%'" call uninstall /nointeractive

PowerShell 静默卸载脚本

# 通过名称卸载
$app = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -like "*程序名称*"}
if ($app) {
    $app.Uninstall()
}
# 通过GUID卸载
$app = Get-WmiObject -Class Win32_Product | Where-Object {$_.IdentifyingNumber -eq "{GUID}"}
$app.Uninstall()
# 使用msiexec
Start-Process msiexec.exe -ArgumentList "/x {GUID} /quiet /norestart" -Wait

Linux 环境

通用卸载脚本

#!/bin/bash
# 检查包管理器并卸载
if command -v dpkg &> /dev/null; then
    # Debian/Ubuntu
    sudo dpkg --purge 包名
    # 或
    sudo apt-get remove --purge -y 包名
elif command -v rpm &> /dev/null; then
    # Red Hat/CentOS
    sudo rpm -e 包名
    # 或
    sudo yum remove -y 包名
elif command -v pacman &> /dev/null; then
    # Arch Linux
    sudo pacman -Rns 包名
fi
# 清理残留配置文件
sudo find /etc -name "*包名*" -exec rm -rf {} \;

批量卸载脚本

#!/bin/bash
# 要卸载的程序列表
PROGRAMS=(
    "vim"
    "nano"
    "firefox"
)
for prog in "${PROGRAMS[@]}"; do
    echo "正在卸载 $prog..."
    # 尝试多种包管理器
    if command -v apt-get &> /dev/null; then
        sudo apt-get remove --purge -y "$prog"
    elif command -v yum &> /dev/null; then
        sudo yum remove -y "$prog"
    elif command -v dnf &> /dev/null; then
        sudo dnf remove -y "$prog"
    fi
    echo "$prog 卸载完成"
done

macOS 环境

App Store 应用卸载

#!/bin/bash
# 卸载指定应用
sudo rm -rf "/Applications/应用程序名.app"
# 清理残留文件
rm -rf ~/Library/Preferences/com.公司名.应用名.plist
rm -rf ~/Library/Application Support/应用名
rm -rf ~/Library/Caches/com.公司名.应用名

使用 brew 卸载

#!/bin/bash
# 卸载 brew 安装的应用
brew uninstall --force 包名
# 清理缓存
brew cleanup -s

高级脚本示例

跨平台智能卸载脚本

#!/usr/bin/env python3
import os
import sys
import platform
import subprocess
def get_os():
    """检测操作系统"""
    system = platform.system().lower()
    if system == "windows":
        return "windows"
    elif system == "linux":
        return "linux"
    elif system == "darwin":
        return "macos"
    else:
        return "unknown"
def uninstall_windows(app_name):
    """Windows静默卸载"""
    try:
        # 使用wmic
        cmd = f'wmic product where "name like \'%{app_name}%\'" call uninstall /nointeractive'
        subprocess.run(cmd, shell=True, check=False)
        # 如果失败,尝试msiexec
        print(f"正在尝试卸载: {app_name}")
    except Exception as e:
        print(f"卸载失败: {e}")
def uninstall_linux(app_name):
    """Linux静默卸载"""
    try:
        # Debian/Ubuntu
        if os.path.exists('/usr/bin/apt-get'):
            subprocess.run(['sudo', 'apt-get', 'remove', '--purge', '-y', app_name])
        # Red Hat/CentOS
        elif os.path.exists('/usr/bin/yum'):
            subprocess.run(['sudo', 'yum', 'remove', '-y', app_name])
        # Arch
        elif os.path.exists('/usr/bin/pacman'):
            subprocess.run(['sudo', 'pacman', '-Rns', app_name])
    except Exception as e:
        print(f"卸载失败: {e}")
def uninstall_macos(app_name):
    """macOS静默卸载"""
    try:
        # 尝试brew
        subprocess.run(['brew', 'uninstall', '--force', app_name], check=False)
        # 删除.App
        app_path = f"/Applications/{app_name}.app"
        if os.path.exists(app_path):
            subprocess.run(['sudo', 'rm', '-rf', app_path])
    except Exception as e:
        print(f"卸载失败: {e}")
def main():
    if len(sys.argv) < 2:
        print("用法: python uninstall.py <程序名称>")
        sys.exit(1)
    app_name = sys.argv[1]
    os_type = get_os()
    print(f"检测到操作系统: {os_type}")
    print(f"正在静默卸载: {app_name}")
    if os_type == "windows":
        uninstall_windows(app_name)
    elif os_type == "linux":
        uninstall_linux(app_name)
    elif os_type == "macos":
        uninstall_macos(app_name)
    else:
        print("不支持的操作系统")
        sys.exit(1)
    print("卸载完成!")
if __name__ == "__main__":
    main()

使用建议

  1. 获取产品GUID:安装并使用 UninstallView 查看
  2. 测试环境:先在测试环境验证脚本
  3. 添加日志:记录卸载过程中的错误信息
  4. 添加备份:卸载前建议备份重要配置

安全注意事项

⚠️ 重要提醒

  • 静默卸载可能导致数据丢失
  • 某些程序需要先关闭服务
  • 建议在卸载前创建系统还原点
  • 确保有管理员权限

请根据您的具体需求和系统环境选择合适的脚本。

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