我正在尝试从批处理文件中调用powershell脚本。
我有以下
for /F "usebackq delims=" %%a in (
'PowerShell.exe -ExecutionPolicy ByPass -Command "(@(Select-String %QUERY% %FILE% -Context 0, 1 | % {$_.Context.PostContext} ))"'
) do set RESULT=%%a
echo %RESULT%
但是%RESULT%
正在打印整个命令而不是结果。
我要做的就是读取一个标志文件,然后将下一行作为%RESULT%
我从命令行运行以下命令
PowerShell.exe -ExecutionPolicy ByPass -Command "& (Select-String FAILED e:\path\to\app.log -Context 0, 1 | % {$_.Context.PostContext} )"
并得到了这个
& : The term ':generic/generic.k4.csv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ & (Select-String FAILED e:\path\to\app.log -Context 0 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:generic/generic.k4.csv:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
答案 0 :(得分:2)
第一个代码块中的问题是使用usebackq
令牌,同时仍然将命令包装在单引号中,强制命令被解释为文字字符串(然后将其放入{ {1}})。来自%RESULT%
usebackq - 指定新语义有效, 其中后引用字符串作为a执行 命令和单引号字符串是一个 文字字符串命令并允许使用 双引号引用文件名 文件集。
要解决此问题,请将命令包装在反引号(for /?
)中,而不是单引号(`
),或从命令中删除'
令牌。强>
第二个PowerShell块与第一个不同。语法usebackq
计算括号内的表达式,然后尝试执行它。在这种情况下,正在评估您的& (expr)
,并返回选定的行。 PowerShell正在尝试执行提取的行,就像它是一个命令一样。这可能不是你想要的。
如需进一步分析,请提供有关您希望在此处发生的事情的更多说明,以及app.log的内容示例。