Simple script:
"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Continue"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
"hello" | Out-Host
I have this output:
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
Why I get "hello" in output ?? -ErrorAction Stop not work??
update:
this code:
"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Stop"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt"
"hello" | Out-Host
worked as expected. there isn't "hello" in output.
Copy-Item ignore -ErrorAction and use only $ErrorActionPreference ??
As indicated by the help the ErrorAction parameter has no effect on terminating errors, which yours is.
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.
Source: Get-Help about_commonparameters
and http://technet.microsoft.com/en-us/library/dd315352.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With