如何创建一个function 是的文件A能替换多个服务器上的文件B?
实现: xx.ps1 -action replace -serverlist sl.txt -path \”c:\\a\\B.txt\” -file A.txt
param( [string]$Action='replace', [string]$ServerListFile = '', [string]$Path = 'MyShare\B.txt', [string]$File = 'A.txt' ) $servers = Get-Content $ServerListFile foreach ($server in $servers) { #新文件内容 $content = Get-Content $File -Raw #服务器共享文件路径 $remoteFilePath = "\\{0}\{1}" -f $server,$Path Write-Host "开始替换: $remoteFilePath" $content| Out-File $remoteFilePath }
Hi Mooser
我根据你的script做了些,修改
param(
[string]$Action,
[string]$ServerList,
[string]$ResourcePath,
[string]$TargetPath,
[string]$File
)
$servers = Get-Content $ServerList
foreach ($server in $servers)
{
#新文件内容
$content = Get-Content $ResourcePath\$File -Raw
#服务器共享文件路径
$remoteFilePath = “\\{0}\{1}” -f $server,$TargetPath
Write-Host “Start to backup the orignal file: $remoteFilePath\$File”
Dir $remoteFilePath | foreach-object -echo $File -process {Rename-Item -newname Backup_$_name}
Write-Host “开始替换: $remoteFilePath”
$content| Out-File $remoteFilePath\$File -Force
}
但是还是有些疑问,在rename原有文件的时候,参数echo是错误的,我怎样从targetpath中筛选出,和file一样的文件进行重命名呢? 另外可否自动重命名成$_Name_old.txt 的形式呢?