简单的脚本:
"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 ??
我尝试将 System.ComponentModel.DefaultValueAttribute 添加到 RuntimeDefinedParameter 的 AttributeCollection,但它不起作用。
powershell ×2