如何将一个文件夹下的多个文件,替换到目标服务器的指定路径内?


PowerShell交流中心分类: 变量-函数-脚本-条件-循环-参数如何将一个文件夹下的多个文件,替换到目标服务器的指定路径内?
0
Elijah0221 asked 8 年 ago

各位大神,新年好
请教一下各位,我有如下一个script,来实现替换多个远端服务器上的多个文件,但是现在必须要写一个参数$File,来指定要替换的文件,如何实现,制定一个resource文件夹路径,让他自动将里面的所有文件去替换目标服务器路径上的对应文件呢?
param(
[string]$Action,
[string]$ServerList,
[string]$ResourcePath,
[string]$TargetPath,
[string]$File
)
$servers = Get-Content $ServerList
foreach ($server in $servers)
{
 #Get the new file content
 $content = Get-Content $ResourcePath\$File -Raw
 
 #Generate te targert folder path
 $remoteFilePath = “\\{0}\{1}” -f $server,$TargetPath
 Write-Host “Start to backup the orignal file: $remoteFilePath\$File”
 
 Rename-Item -path $remoteFilePath\$File -newname Backup_$File -ErrorAction SilentlyContinue
 if($?)
    {
     Write-Host -ForegroundColor Green “Backup Completed, Start to replace the file to: $remoteFilePath\$File”
     $content| Out-File $remoteFilePath\$File -Force -ErrorAction SilentlyContinue
    if($?)
         {
        Write-Host -ForegroundColor Green “Replace Completed”
         }
         else
         {
        Write-Warning $Error[0].Exception.Message
        Write-Warning “Replace Stopped with error”
         }
    }
 else
    {
    Write-Warning $Error[0].Exception.Message
    Write-Warning “Backup Stopped with error”
    }
 
}