我有这样一个script,请问,如何实现,当出现问题时,就终止,而不会继续进行下一条语句呢?
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
Write-Host “Start to replace the file to: $remoteFilePath\$File”
$content| Out-File $remoteFilePath\$File -Force
}
根据上面的设置,在rename的时候,如果Backup_$File存在的话,会出错,但是,系统还是会继续下面的替换操作,导致原有的文件丢失。
请各位大神,帮忙完善,使得更加人性化。谢谢。。。