Powershell没有在try / catch中捕获异常?

时间:2013-12-03 22:02:45

标签: c# powershell try-catch acl ntfs

我有一段代码如下:
...
32: try {
33: set-acl -aclObject $AC -path $o.fullname
34: } catch [System.UnauthorizedAccessException] {
...

在执行期间,我得到了这个:

    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

作为系统运行。 知道为什么没有被捕获吗?

1 个答案:

答案 0 :(得分:3)

尝试将ErrorAction参数设置为Stop

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