从powershell在新窗口中调用bat文件

Con*_*eis 3 windows powershell cmd batch-file

我想从 powershell 内的新窗口/独立实例中启动 bat 文件。

bat 文件将从 powershell 获取计算机名,
例如

mybat.bat $thiscomputer
Run Code Online (Sandbox Code Playgroud)

我试过了

start mybat.bat $thiscomputer
start "cmd /c" mybat.bat $thiscomputer
start /k mybat.bat $thiscomputer
start-process mybat.bat $thiscomputer
Run Code Online (Sandbox Code Playgroud)

通常,bat 确实会运行,但保留在 powershell 脚本内。

我需要 powershell 脚本在新窗口中启动 bat 并循环回到开头。

谢谢

孔夫塞斯

Rya*_*ose 6

Start-Process默认情况下在新窗口中运行控制台程序。您的语法的唯一问题是您需要使用-ArgumentList参数将参数传递给您启动的程序。PowerShell 不允许您简单地将它们列在程序名称后面。

PS> Start-Process $env:comspec -ArgumentList "mybat.bat $thiscomputer"
Run Code Online (Sandbox Code Playgroud)

或者,用更短的形式,

PS> start mybat.bat -a $thiscomputer
Run Code Online (Sandbox Code Playgroud)

-ArgumentList只接受一个字符串,因此如果您要传递多个参数,那么您需要将它们全部放入一个字符串中,然后再传递给Start-Process.