繁体   English   中英

powershell:日志文件写入不正确

[英]powershell: log file is not written correctly

我需要以下脚本方面的帮助。 我想将文件夹复制到多台计算机并调整权限。 应该记录复制过程是否成功以及权限设置是否成功。 然而,对于第二个,它写入日志文件它是不成功的,尽管它是并且设置了权限。

$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
}
}

这可能与使用try{} catch{}块而不使用ErrorAction Stop

在这种情况下,将代码包装在里面是最简单的:

$oldErrorAction = $ErrorActionPreference
$ErrorActionPreference = 'Stop'

# your code

$ErrorActionPreference = $oldErrorAction

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM