【问题标题】:How to start cmd from within powershell and pass a command to the cmd instance?如何从 powershell 中启动 cmd 并将命令传递给 cmd 实例?
【发布时间】:2021-04-09 22:14:21
【问题描述】:

我正在尝试每分钟从 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 上发布的this 答案,但是我不清楚我做错了什么。

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

【问题讨论】:

  • 试试cmd.exe /k "node index.js",不带start cmdlet。
  • start 是 PowerShell 中 Start-Process 的别名。您是否查看了来自help Start-Process -Full 的信息?您使用的是哪个版本的 PowerShell? $PSVersionTable.PSVersion.ToString()
  • @AbrahamZinala 不幸的是,这不起作用,因为它不会像之前有 start 那样在新窗口中创建新的 cmd 实例
  • 请特别参阅help Start-Process -Full PowerShell 7.1.2 中的示例 7。
  • 类似于亚伯拉罕的评论,我可能会尝试用& cmd /D /K "node index.js"替换start cmd.exe /k node index.js

标签: powershell cmd


【解决方案1】:

Start 是 @lit

提到的 Start-Process cmdlet 的别名

任何参数都必须通过 -ArgumentList 参数传递。

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

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2018-12-07
    • 2018-04-11
    • 2020-01-16
    • 2021-05-27
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多