gpt4 book ai didi

powershell - -ErrorAction Stop 在 Copy-Item 中不起作用

转载 作者:行者123 更新时间:2023-12-03 16:24:13 25 4
gpt4 key购买 nike

简单的脚本:

"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Continue"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
"hello" | Out-Host

我有这个输出:
Copy-Item : Could not find a part of the path "C:\NonExistingDir\file.txt".
C:\Users\ESavin\AppData\Local\Temp\d3d410e0-79b3-4736-b7e7-5aba1ab11a12.ps1:1 знак:10
+ Copy-Item <<<< "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
+ CategoryInfo : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand

hello

为什么我在输出中得到“你好”? -ErrorAction 停止不起作用??

更新:

这段代码:
"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Stop"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt"
"hello" | Out-Host

按预期工作。输出中没有“你好”。

Copy-Item 忽略 -ErrorAction 并仅使用 $ErrorActionPreference ??

最佳答案

正如帮助中所指出的,ErrorAction 参数对终止错误没有影响,而您的错误是。

    The ErrorAction parameter has no effect on terminating errors (such as
missing data, parameters that are not valid, or insufficient
permissions) that prevent a command from completing successfully.

来源: Get-Help about_commonparametershttp://technet.microsoft.com/en-us/library/dd315352.aspx

关于powershell - -ErrorAction Stop 在 Copy-Item 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9357175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com