小编Jac*_*128的帖子

-ErrorAction停止在Copy-Item中工作

简单的脚本:

"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Continue"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
"hello" | Out-Host
Run Code Online (Sandbox Code Playgroud)

我有这个输出:

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
Run Code Online (Sandbox Code Playgroud)

为什么我在输出中得到"你好"?-ErrorAction停止不工作?

更新:

这段代码:

"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Stop"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt"
"hello" | Out-Host
Run Code Online (Sandbox Code Playgroud)

按预期工作.输出中没有"你好".

Copy-Item ignore -ErrorAction并仅使用$ ErrorActionPreference ??

powershell

6
推荐指数
2
解决办法
6881
查看次数

如何设置动态参数的默认值?

我尝试将 System.ComponentModel.DefaultValueAttribute 添加到 RuntimeDefinedParameter 的 AttributeCollection,但它不起作用。

powershell

6
推荐指数
1
解决办法
4973
查看次数

标签 统计

powershell ×2