You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求补全远程卸载应用PowerShell脚本的备份与回退代码

补充的PowerShell代码:应用配置备份与故障回退

一、应用配置与注册表备份模块

以下代码实现针对Notepad++、Chrome的用户配置文件及关联注册表项的远程备份,可直接整合进现有脚本的卸载前置流程:

function Backup-AppConfigs {
    param(
        [Parameter(Mandatory=$true)]
        [string[]]$ComputerNames,
        [Parameter(Mandatory=$true)]
        [string[]]$TargetApps,
        [string]$BackupRootPath = "\\CentralServer\Backup\AppUninstall"
    )

    foreach ($computer in $ComputerNames) {
        try {
            # 初始化远程会话
            $session = New-PSSession -ComputerName $computer -ErrorAction Stop
            $backupPath = Join-Path -Path $BackupRootPath -ChildPath "$computer\_$(Get-Date -Format 'yyyyMMddHHmmss')"
            
            Invoke-Command -Session $session -ArgumentList $TargetApps, $backupPath -ScriptBlock {
                param($apps, $localBackupPath)
                
                # 创建本地备份目录
                if (-not (Test-Path $localBackupPath)) {
                    New-Item -Path $localBackupPath -ItemType Directory | Out-Null
                }

                foreach ($app in $apps) {
                    switch ($app) {
                        "Notepad++" {
                            # 备份Notepad++配置文件
                            $npConfigPath = Join-Path -Path $env:APPDATA -ChildPath "Notepad++"
                            if (Test-Path $npConfigPath) {
                                Copy-Item -Path $npConfigPath -Destination (Join-Path $localBackupPath "Notepad++") -Recurse -Force
                            }

                            # 备份Notepad++注册表项(32/64位)
                            $regPaths = @(
                                "HKLM:\SOFTWARE\Notepad++",
                                "HKLM:\SOFTWARE\Wow6432Node\Notepad++"
                            )
                            foreach ($regPath in $regPaths) {
                                if (Test-Path $regPath) {
                                    $regBackupPath = Join-Path $localBackupPath "$($regPath.Replace(':', '_')).reg"
                                    reg export $regPath $regBackupPath /y | Out-Null
                                }
                            }
                        }
                        "Chrome" {
                            # 备份Chrome用户配置(默认用户目录)
                            $chromeConfigPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Google\Chrome\User Data\Default"
                            if (Test-Path $chromeConfigPath) {
                                Copy-Item -Path $chromeConfigPath -Destination (Join-Path $localBackupPath "Chrome_Default_User") -Recurse -Force
                            }

                            # 备份Chrome注册表项
                            $regPaths = @(
                                "HKLM:\SOFTWARE\Google\Chrome",
                                "HKLM:\SOFTWARE\Wow6432Node\Google\Chrome"
                            )
                            foreach ($regPath in $regPaths) {
                                if (Test-Path $regPath) {
                                    $regBackupPath = Join-Path $localBackupPath "$($regPath.Replace(':', '_')).reg"
                                    reg export $regPath $regBackupPath /y | Out-Null
                                }
                            }
                        }
                    }
                }
            }

            Write-Log -Message "成功完成$computer上的应用配置备份,备份路径:$backupPath" # 复用现有日志函数
            Remove-PSSession -Session $session
        }
        catch {
            Write-Log -Message "$computer备份失败:$($_.Exception.Message)" -Level Error # 复用现有日志函数
            if ($session) { Remove-PSSession -Session $session }
        }
    }
}

使用说明

  1. 在现有脚本的卸载逻辑之前调用此函数:Backup-AppConfigs -ComputerNames $ServerList -TargetApps $AppList
  2. BackupRootPath可指定中央共享目录,确保远程服务器有读写权限
  3. 备份文件按服务器名+时间戳命名,避免覆盖

二、故障状态回退模块

以下代码实现卸载失败时,恢复之前备份的配置文件与注册表项,需配合现有脚本的异常捕获逻辑调用:

function Restore-AppConfigs {
    param(
        [Parameter(Mandatory=$true)]
        [string]$ComputerName,
        [Parameter(Mandatory=$true)]
        [string[]]$TargetApps,
        [Parameter(Mandatory=$true)]
        [string]$BackupPath
    )

    try {
        $session = New-PSSession -ComputerName $ComputerName -ErrorAction Stop
        
        Invoke-Command -Session $session -ArgumentList $TargetApps, $BackupPath -ScriptBlock {
            param($apps, $localBackupPath)

            foreach ($app in $apps) {
                switch ($app) {
                    "Notepad++" {
                        # 恢复Notepad++配置文件
                        $npConfigTarget = Join-Path -Path $env:APPDATA -ChildPath "Notepad++"
                        $npConfigBackup = Join-Path -Path $localBackupPath -ChildPath "Notepad++"
                        if (Test-Path $npConfigBackup) {
                            Copy-Item -Path $npConfigBackup -Destination $npConfigTarget -Recurse -Force
                        }

                        # 恢复Notepad++注册表项
                        $regBackupFiles = Get-ChildItem -Path $localBackupPath -Filter "*Notepad++*.reg"
                        foreach ($regFile in $regBackupFiles) {
                            reg import $regFile.FullName | Out-Null
                        }
                    }
                    "Chrome" {
                        # 恢复Chrome用户配置
                        $chromeConfigTarget = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Google\Chrome\User Data\Default"
                        $chromeConfigBackup = Join-Path -Path $localBackupPath -ChildPath "Chrome_Default_User"
                        if (Test-Path $chromeConfigBackup) {
                            Copy-Item -Path $chromeConfigBackup -Destination $chromeConfigTarget -Recurse -Force
                        }

                        # 恢复Chrome注册表项
                        $regBackupFiles = Get-ChildItem -Path $localBackupPath -Filter "*Chrome*.reg"
                        foreach ($regFile in $regBackupFiles) {
                            reg import $regFile.FullName | Out-Null
                        }
                    }
                }
            }
        }

        Write-Log -Message "成功完成$ComputerName上的应用配置回退,备份来源:$BackupPath" # 复用现有日志函数
        Remove-PSSession -Session $session
    }
    catch {
        Write-Log -Message "$ComputerName回退失败:$($_.Exception.Message)" -Level Error # 复用现有日志函数
        if ($session) { Remove-PSSession -Session $session }
    }
}

使用说明

  1. 在现有脚本的卸载异常捕获块中调用此函数,需传入对应服务器的备份路径:
# 示例:现有卸载逻辑的异常处理部分
try {
    # 现有卸载代码
}
catch {
    $errorMsg = $_.Exception.Message
    Write-Log -Message "$computer卸载失败:$errorMsg" -Level Error
    # 调用回退
    Restore-AppConfigs -ComputerName $computer -TargetApps $currentApp -BackupPath $对应的备份路径
}
  1. 需确保卸载前已成功执行备份,且备份路径可被远程服务器访问
  2. 注册表恢复使用reg import命令,兼容32/64位系统

内容的提问来源于stack exchange,提问作者MikeRej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:42:42