PowerShell: ErrorAction set to "SilentlyContinue" not working
The following command does not show the error message, which is wha开发者_如何学运维t I want:
Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -ErrorAction SilentlyContinue
The following command does show the error message, which is not what I want:
Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -Force -ErrorAction SilentlyContinue
This is because I'm using the "Force" parameter. Is there a way I can use the "Force" parameter and still not show the error message?
Add this first.
$ErrorActionPreference = "silentlycontinue"
Can you try this :
trap
{
continue
}
Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -Force -errorAction SilentlyContinue
or
try
{
Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -Force -errorAction SilentlyContinue
}
catch
{
}
I just happen to run across your post as I was searching for an answer to mine. This post seems to resolve to it being a bug, although this one is talking about the "Verbose" parameter, it could apply to -force as well.
https://social.technet.microsoft.com/Forums/windowsserver/en-US/b76eccae-4484-43ec-a3dc-d4bc581124c2/adding-verbose-to-a-cmdlet-prevents-script-from-terminating-on-error?forum=winserverpowershell
精彩评论