在Powershell中执行批处理文件 [英] Execute batch file in Powershell

查看:244
本文介绍了在Powershell中执行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to execute the following from a 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用法

Now I would like to log the output of the process to a file for which I came across the quite handy powershell usage of

powershell "dir | tee output.log"

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

but this does not take my batch file as first argument (powershell "my.bat | tee output.log") because it is not the name of a cmdlet or a function or a script file.

我可以更改我的批处理文件,使之表示为 powershell"OpenCover.Console.exe ..." ,但是我必须修改所有引号并更改转义字符,等等.

I could change my batch file so that is says powershell "OpenCover.Console.exe..." but I would have to adapt all quotes and change escape characters and so forth.

是否有一种方法可以使批处理文件在Powershell中执行?还是有某种方法可以在执行某些powershell命令后从批处理中直接删除我的行,并且全部执行应有的操作"?

Is there a way to make a batch file execute in powershell? Or is there a way to drop in my line unchanged from the batch after some powershell command and it all executes "like it ought to"?

推荐答案

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

Unless your batch file is in a folder in the %PATH%, PowerShell won't find it [1], so you'll have to supply an explicit file path (whether relative or absolute).

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

For instance, if the batch file is in the current folder, run:

powershell -c ".\my.bat | tee output.log"

考虑添加 -noprofile 来禁止加载配置文件,通常仅在 interactive 会话中才需要.

Consider adding -noprofile to suppress loading of the profile files, which is typically only needed in interactive sessions.

如果批处理文件路径包含嵌入式空格,请将其用单引号引起来并在& 之前加上:

If your batch file path contains embedded spaces, enclose it in single quotes and prepend &:

powershell -c "& '.\my script.bat' | tee output.log"

注意:我特意在上面添加了 -c ( -Command 的缩写)参数名;而 powershell.exe - Windows PowerShell -默认为该参数,在 PowerShell [Core] v6 + 中不再适用(其可执行文件名为 pwsh ),其中 -File 现在是默认设置-请参见

Note: I've deliberately added the -c (short for: -Command) parameter name above; while powershell.exe - Windows PowerShell - defaults to this parameter, that is no longer true in PowerShell [Core] v6+ (whose executable name is pwsh), where -File is now the default - see about_PowerShell.exe and about_pwsh

[1]更准确地说,PowerShell-与 cmd.exe 不同-将设计为不会执行 current 文件夹中的脚本通过它们的纯文件名(在交互式PowerShell会话中,您将获得有关该效果的提示).这是一项安全功能,旨在防止意外调用与预期不同的可执行文件.

[1] More accurately, PowerShell - unlike cmd.exe - will by design not execute scripts in the current folder by their mere filename (in an interactive PowerShell session you'll get a hint to that effect). This is a security feature designed to prevent accidental invocation of a different executable than intended.

这篇关于在Powershell中执行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆