Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell中执行批处理文件_Powershell_Batch File - Fatal编程技术网

在Powershell中执行批处理文件

在Powershell中执行批处理文件,powershell,batch-file,Powershell,Batch File,我想从批处理文件执行以下操作: "C:\OpenCover\tools\OpenCover.Console.exe" -register:user -target:"%VS110COMNTOOLS%..\IDE\mstest.exe" -targetargs:"/testcontainer:\"C:\Develop\bin\Debug\MyUnitTests.dll\" ... " PAUSE 现在,我想将进程的输出记录到一个文件中,因为我发现了非常方便的powershell的用法 power

我想从批处理文件执行以下操作:

"C:\OpenCover\tools\OpenCover.Console.exe" -register:user -target:"%VS110COMNTOOLS%..\IDE\mstest.exe" -targetargs:"/testcontainer:\"C:\Develop\bin\Debug\MyUnitTests.dll\" ... "
PAUSE
现在,我想将进程的输出记录到一个文件中,因为我发现了非常方便的powershell的用法

powershell "dir | tee output.log"
但这不会将我的批处理文件作为第一个参数(
powershell“my.bat | tee output.log”
),因为它不是cmdlet或函数或脚本文件的名称

我可以更改批处理文件,使其显示为
powershell“OpenCover.Console.exe…”
,但我必须调整所有引号并更改转义字符等等


有没有办法让批处理文件在powershell中执行?或者有没有一种方法可以在某些powershell命令执行后,从批处理中删除未更改的my line(我的行),并且所有这些命令都按“应该”的方式执行?

除非批处理文件位于
%PATH%
的文件夹中,否则powershell将找不到它[1],因此您必须提供一个明确的文件路径(无论是相对路径还是绝对路径)

例如,如果批处理文件位于当前文件夹中,请运行:

powershell -c ".\my.bat | tee output.log"
考虑添加
-noprofile
以抑制配置文件的加载,这通常仅在交互式会话中需要

如果批处理文件路径包含嵌入空格,请将其括在单引号中,并在前面加上前缀
&

powershell -c "& '.\my script.bat' | tee output.log"
注意:我特意在上面添加了
-c
(缩写:
-Command
)参数名;虽然
powershell.exe
-Windows powershell-默认为此参数,但在powershell[Core]v6+(其可执行文件名为
pwsh
)中,此参数不再适用,其中
-File
现在是默认值-请参阅和



[1] 更准确地说,PowerShell不同于
cmd.exe
,它在设计上不会仅仅通过文件名来执行当前文件夹中的脚本(在交互式PowerShell会话中,您会得到这样的提示)。这是一项安全功能,旨在防止意外调用与预期不同的可执行文件。

除非OP中没有说明这样做的目的,否则没有理由同时使用Powershell和批处理脚本。如果希望仅从PS执行此操作,可以创建一个PS脚本,该脚本执行批处理文件的所有操作

为了避免转义问题(或者利用CMD.EXE有点奇怪的转义行为:-),您可以使用PS3.0中引入的
-->%
。它记录在“关于转义字符”下