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

基于CSV的新旧远程服务器文件迁移脚本完善需求

完整PowerShell文件跨服务器迁移实现方案

1. 先更新服务器对应关系CSV

把原有的C:\Temp\Servers.csv补充New_Server列,格式如下:

Old_Server,New_Server
OldServer01,NewServer01
OldServer02,NewServer02
FinanceOld01,FinanceNew01

2. 完整迁移脚本(含本地到新服务器同步逻辑)

以下脚本整合了你已实现的「旧服务器→本地」复制逻辑,补充了「本地→新服务器」的同步功能,同时加入了连接检查、错误捕获和日志记录:

# 可配置参数
$csvPath = "C:\Temp\Servers.csv"
$oldServerSourcePath = "D:\BusinessData" # 旧服务器上的源文件根路径
$localTempRoot = "C:\Temp\MigrationCache" # 本地临时存储根目录
$newServerTargetPath = "D:\MigratedBusinessData" # 新服务器上的目标根路径
$logFile = "C:\Temp\Migration_$(Get-Date -Format 'yyyyMMdd').log"

# 初始化本地临时目录和日志
if (-not (Test-Path $localTempRoot)) { New-Item $localTempRoot -ItemType Directory | Out-Null }
if (Test-Path $logFile) { Clear-Content $logFile }

# 读取服务器对应关系
$serverPairs = Import-Csv $csvPath

foreach ($pair in $serverPairs) {
    $oldServer = $pair.Old_Server
    $newServer = $pair.New_Server
    $localServerCache = Join-Path $localTempRoot $oldServer

    # --------------------------
    # 第一步:旧服务器→本地复制
    # --------------------------
    try {
        Write-Host "[$(Get-Date -Format 'HH:mm:ss')] 开始从 $oldServer 拉取文件..." -ForegroundColor Cyan
        # 先检查旧服务器连通性
        if (-not (Test-Connection $oldServer -Count 1 -Quiet)) {
            throw "旧服务器无法ping通"
        }
        # 创建当前服务器的本地缓存目录
        if (-not (Test-Path $localServerCache)) { New-Item $localServerCache -ItemType Directory | Out-Null }
        # 使用Robocopy复制(支持断点续传、子目录)
        $robocopyPullArgs = @(
            "\\$oldServer\$($oldServerSourcePath.Replace(':','$'))",
            $localServerCache,
            "/E", "/R:3", "/W:5", "/LOG+:$logFile", "/NFL", "/NDL"
        )
        Start-Process robocopy.exe -ArgumentList $robocopyPullArgs -Wait -NoNewWindow
        Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $oldServer 拉取完成" -ForegroundColor Green
    }
    catch {
        $errMsg = "[$(Get-Date -Format 'HH:mm:ss')] $oldServer 拉取失败: $_"
        Write-Host $errMsg -ForegroundColor Red
        Add-Content $logFile $errMsg
        continue # 跳过当前服务器,继续处理下一个
    }

    # --------------------------
    # 第二步:本地→新服务器同步
    # --------------------------
    try {
        Write-Host "[$(Get-Date -Format 'HH:mm:ss')] 开始同步文件到 $newServer..." -ForegroundColor Cyan
        # 检查新服务器连通性
        if (-not (Test-Connection $newServer -Count 1 -Quiet)) {
            throw "新服务器无法ping通"
        }
        # 拼接新服务器的共享路径
        $newServerTargetShare = "\\$newServer\$($newServerTargetPath.Replace(':','$'))"
        # 确保目标路径存在
        if (-not (Test-Path $newServerTargetShare)) { New-Item $newServerTargetShare -ItemType Directory | Out-Null }
        # 镜像同步(目标与本地缓存完全一致,删除多余文件)
        $robocopyPushArgs = @(
            $localServerCache,
            $newServerTargetShare,
            "/MIR", "/R:3", "/W:5", "/LOG+:$logFile", "/NFL", "/NDL"
        )
        Start-Process robocopy.exe -ArgumentList $robocopyPushArgs -Wait -NoNewWindow
        Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $newServer 同步完成" -ForegroundColor Green

        # 可选:同步完成后清理本地缓存(节省磁盘空间)
        # Remove-Item $localServerCache -Recurse -Force
    }
    catch {
        $errMsg = "[$(Get-Date -Format 'HH:mm:ss')] $newServer 同步失败: $_"
        Write-Host $errMsg -ForegroundColor Red
        Add-Content $logFile $errMsg
    }
}

Write-Host "`n所有迁移任务执行完毕,详细日志请查看: $logFile" -ForegroundColor Yellow

3. 关键细节说明

  • CSV对应关系:通过CSV批量管理新旧服务器的映射,新增/修改服务器只需编辑CSV,无需改动脚本
  • Robocopy优势:相比原生Copy-Item,支持断点续传、错误重试、镜像同步(确保目标与源完全一致),日志功能更完善
  • 错误隔离:单个服务器的复制/同步失败不会终止整个脚本,后续服务器仍会继续处理
  • 本地缓存隔离:为每个旧服务器创建独立的本地缓存目录,避免不同服务器的文件冲突
  • 日志记录:所有操作日志写入指定文件,方便事后排查迁移异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 00:50:48