我在下面的脚本方面需要帮助。 我想将文件夹复制到多台计算机并调整权限。应记录复制过程是否成功以及权限设置是否成功。然而,对于第二个,它会向日志文件写入失败,尽管它成功了并且权限已设置。
$file = get-content -path "C:\temp\pc.txt"
foreach($pc in $file) {
try {
copy-item -path "\\server1\folder1" -Destination "\\$pc\C$\temp\" -force -recurse -verbose
Write-Output $([string](get-date) + "`t $pc success") | out-file -append -filepath "C:\temp\sucess.log"
}
catch {Write-Output $([string](get-date) + "`t $pc failed") | out-file -append -filepath "C:\temp\failed.log"
}
foreach($pc in $file) {
try {
$acl = get-acl -path "\\$pc\c$\temp\folder1"
$new = "users","full","ContainerInherit,ObjectInherit","None","Allow"
$accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $new
$acl.SetAccessRule($accessRule)
$acl | Set-Acl "\\$pc\c$\temp\folder1"
Write-Output $([string](get-date) + "`t $pc success") | out-file -append -filepath "C:\temp\acl_sucess.log"
}
catch {Write-Output $([string](get-date) + "`t $pc failed") | out-file -append -filepath "C:\temp\acl_failed.log"
}
write-host
write-host "see Log" -foreground "green"
write-host
}
}
答案 0 :(得分:1)
这可能与使用 try{} catch{}
块而不使用 ErrorAction Stop
有关。
在这种情况下,最简单的方法是将代码包装在其中:
$oldErrorAction = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
# your code
$ErrorActionPreference = $oldErrorAction