Reputation: 81
I need help with my script below. I want to copy folders to multiple computers and adjust the permissions. It should be logged whether the copying process was successful and whether the setting of the authorizations was successful. With the second, however, it writes to the log file that it was unsuccessful, although it was and the permissions were set.
$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
}
}
Upvotes: 0
Views: 29
Reputation: 61243
This probably has to do with using try{} catch{}
blocks without using ErrorAction Stop
.
In this case, it would be easiest to wrap your code inside:
$oldErrorAction = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
# your code
$ErrorActionPreference = $oldErrorAction
Upvotes: 1