kịch bản đơn giản:-ErrorAction Dừng không làm việc trong Copy-mục
"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Continue"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
"hello" | Out-Host
Tôi có đầu ra này:
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
Tại sao tôi nhận được "hello" trong đầu ra ?? -ErrorAction Stop không hoạt động?
update:
mã này:
"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Stop"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt"
"hello" | Out-Host
làm việc như mong đợi. không có "hello" trong đầu ra.
Sao chép mục bị bỏ qua -Lỗi hành động và chỉ sử dụng $ ErrorActionPreference ??
Bạn phải tạo ra "C: \ existing_file.txt" trên máy tính của bạn để tái tạo ví dụ của tôi – Jack128