2
votes

I have a piece of code like:
...
32: try {
33: set-acl -aclObject $AC -path $o.fullname
34: } catch [System.UnauthorizedAccessException] {
...

During execution I get this:

    set-acl : Attempted to perform an unauthorized operation.
    At C:\script.ps1:33 char:3
    +         set-acl -aclObject $AC -path $o.fullname
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (N:\some_path:String) [Set-Acl], UnauthorizedAccessException
        + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetAclCommand

Running as system. Any idea why it's not being captured?

1
A workaround is to check $? and use IFMaciej Wakuła
Are you running this remotely via invoke-command, by any chance?x0n

1 Answers

3
votes

Try setting the ErrorAction parameter to Stop:

set-acl -aclObject $AC -path $o.fullname -ErrorAction Stop