ErrorAction SilentlyContinue在一元运算符后缺少表达式

时间:2013-12-19 12:25:55

标签: batch-file powershell

我想要做的是禁止错误/警告消息并继续执行我的批处理脚本。所以我正在做的是这样的:

SET psh_path="%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"

%psh_path% -ExecutionPolicy ByPass -ErrorAction "SilentlyContinue" -Command "Test_invalid_command;"

但是它给了我错误:

Missing expression after unary operator '-'.
At line:1 char:2

我发现解决此问题的一种方法是使用命令

创建silentlyContinue.ps1文件
$ErrorActionPreference = "SilentlyContinue"

然后像这样使用Dot Sourcing:

%psh_path% -ExecutionPolicy ByPass -Command ". '.\silentlyContinue.ps1'; Test_invalid_command;"

我关心的是如何在第一种情况下实现这一点,我的意思是将选项传递给-ErrorAction

2 个答案:

答案 0 :(得分:2)

-ErrorAction不是Powershell.exe的参数

运行powershell.exe /?获取它的参数列表。

-ErrorAction设置需要包含在命令脚本中,无论是显式还是源自其他脚本。

答案 1 :(得分:0)

检查一下:

SET psh_path="%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"

%psh_path% -ExecutionPolicy ByPass -Command "Test_invalid_command" -ErrorAction SilentlyContinue
相关问题