【问题标题】:Try Catch still not working in Powershell V2 even with “$ErrorActionPreference = “Stop”即使使用“$ErrorActionPreference = “Stop”,尝试 Catch 仍然无法在 Powershell V2 中工作
【发布时间】:2015-12-25 22:50:01
【问题描述】:

我在三台计算机上运行完全相同的代码。在 PSV2(我不允许升级到 PSV3)的计算机“A”上,除了在执行结束时打印错误消息之外,以下代码不会做任何事情。

$ErrorActionPreference = "Stop"
TRY { $ErrorActionPreference = "Stop"
New-Item -Type File $("C:\Windows\System32\NewFile.Txt") -ErrorAction Stop }
CATCH [System.UnauthorizedAccessException],[System.UnauthorizedAccessException]
{ Write "Rerun with elevated permissions" }
FINALLY { Get-ChildItem $("C:\Windows\System32\new*") }

屏幕一侧的消息说我无法添加图像,所以这变得很有趣......

    PS C:\Temp> $ErrorActionPreference = "Stop"
    PS C:\Temp> TRY { $ErrorActionPreference = "Stop"
    >>   New-Item -Type File $("C:\Windows\System32\NewFile.Txt") -ErrorAction Stop }
    >> CATCH [System.UnauthorizedAccessException],[System.UnauthorizedAccessException] { Write "Rerun with elevated permissions" }
    >> FINALLY { Get-ChildItem $("C:\Windows\System32\new*") }
    >>  

        Directory: C:\Windows\System32  


    Mode                LastWriteTime     Length Name  
    ----                -------------     ------ ----  
    -a---         7/13/2009   9:41 PM     313856 newdev.dll  
    -a---         7/13/2009   9:39 PM      76288 newdev.exe  
    New-Item : Access to the path 'C:\Windows\System32\NewFile.Txt' is denied.
    At line:2 char:15  
    +       New-Item <<<<  -Type File $("C:\Windows\System32\NewFile.Txt") -ErrorAction Stop }  
        + CategoryInfo          : PermissionDenied: (C:\Windows\System32\NewFile.Txt:String) [New-Item], UnauthorizedAccessException  
        + FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand  

无论如何,在分别为 PSV3/PSV4 的计算机“B”和“C”上,它按预期工作,给出以下内容

    PS C:\temp> `$ErrorActionPreference = "Stop"`  
    PS C:\temp> `TRY { $ErrorActionPreference = "Stop"`  
    >>   `New-Item -Type File $("C:\Windows\System32\NewFile.Txt") -ErrorAction Stop }`  
    >> `CATCH [System.UnauthorizedAccessException],[System.UnauthorizedAccessException] { Write "Rerun with elevated permissions" }`  
    >> `FINALLY { Get-ChildItem $("C:\Windows\System32\new*") }`  
    >>  
    Rerun with elevated permissions  


       Directory: C:\Windows\System32  


    Mode                LastWriteTime     Length Name  
    ----                -------------     ------ ----  
    -a---         7/13/2009   9:41 PM     313856 newdev.dll  
    -a---         7/13/2009   9:39 PM      76288 newdev.exe  


    PS C:\temp>  

如您在上面的源代码中所见。除了命令本身的 -Erroraction Stop 之外,$ErrorActionPreference 行位于 try 块之外以及 try 块内部,在 V2 计算机上它不起作用,而在 V3/V4 机器上它是。

超过 98% 的机器是 PSv2,由于尚未有人分享的原因,我们不允许升级它们。

欢迎任何想法或想法。 谢谢,

【问题讨论】:

  • 忽略第二个“屏幕截图”中多余的 ` 标记。除了将这两个部分都作为“非代码”之外,我在获取它时遇到了问题。

标签: powershell-2.0 try-catch-finally


【解决方案1】:

唯一让我印象深刻的是,您在脚本中使用的是“Write”速记而不是完整的“Write-Host”命令。我预计这可能会导致问题,因为您可以在 PowerShell 中执行许多不同的“Write-*”命令。可能是PowerShell的V3和V4有“Write”作为“Write-Host”的简写,而V2没有。

为了清楚起见,这是您的代码的更新示例;

$ErrorActionPreference = "Stop"
TRY { New-Item -Type File $("C:\Windows\System32\NewFile.Txt") }
CATCH [System.UnauthorizedAccessException]
{ Write "Rerun with elevated permissions" }
FINALLY { Get-ChildItem $("C:\Windows\System32\new*") }

我还删除了不必要的 ErrorAction 信息 - 根据您编写的代码,$ErrorActionPreference 只需要设置一次,因为如果首选项设置在 Try/Catch/ 之外,UnauthorizedAccessException 将生成终止错误最后挡住。此外,您应该只需要 Catch 旁边的 [System.UnauthorizedAccessException] 块的一个实例即可使其工作。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多