首页 > 解决方案 > 如何从 powershell 中启动 cmd 并将命令传递给 cmd 实例?

问题描述

我正在尝试每分钟从 powershell 创建一个 windows cmd 终端的实例,最多 8 次,并让每个 cmd 实例从那里运行一个 nodejs 脚本。

到目前为止,这是我的代码:

For ($i=0; $i -le 8; $i++) {
    start cmd.exe /k node index.js
    Start-Sleep -Seconds 60
    }

但我不断收到错误:

Start-Process : A positional parameter cannot be found that accepts argument 'node'.
At C:\Users\user\Documents\x\x\build\src\start.ps1:2 char:5
+     start cmd.exe /k node index.js
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

我已经查看了在 SuperUser 上发布的这个答案,但是,我不清楚我做错了什么。

这个堆栈溢出线程的第二个答案似乎正在做我想做的事情,但我不断收到上述错误。

标签: powershellcmd

解决方案


Start是@lit 提到的Start-Process cmdlet 的别名

任何参数都必须使用-ArgumentList参数传递。

start "cmd.exe"  -ArgumentList "/k node index.js"

推荐阅读