如何从PowerShell运行多个CMD命令提示

发布于 2025-01-22 08:22:06 字数 185 浏览 0 评论 0 原文

因此,我正在尝试编写一个脚本,使我可以打开几个CMD命令提示符并在其中写入相同的命令,但具有不同的变量。

我带来的解决方案是编写一个powershell脚本,该脚本在循环中调用CMD文件,并每次将变量传递到CMD文件,但我被困了,PowerShell脚本仅执行一个CMD。

有人可以帮助解决这个问题吗?

谢谢 :)

So I am trying to write a script that allows me to open several cmd command prompt and write in them the same command but with different variables.

The solution that I came with was to write a PowerShell script that calls inside a loop a cmd file and pass a variable each time to the cmd file but I'm stuck, the PowerShell script execute only one cmd.

Can someone help to figure this out ?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2025-01-29 08:22:06

您可以使用以下内容:

cmd.exe /c [command]

例如

$x = 1..100
foreach ($n in $x){cmd.exe /c ping 192.168.1.$n}

You can use the following :

cmd.exe /c [command]

for example

$x = 1..100
foreach ($n in $x){cmd.exe /c ping 192.168.1.$n}
微暖i 2025-01-29 08:22:06

穆罕默德·赛义德(Mohamed Saeed /em>在当前控制台窗口中。

相比之下,如果您要打开多个 Interactive cmd.exe sessions,异步 ,每个在其独立的新窗口中< /em>,使用 cmd.exe 's /k 选项和Indoke cmd.exe 通过 start-process

# Open three new interactive cmd.exe sessions in new windows 
# and execute a sample command  in each.
'date /t', 'ver', "echo $PSHOME" | ForEach-Object {
  # Parameters -FilePath and -ArgumentList implied.
  Start-Process cmd.exe /k, $_
}

注意:

  • ,除非您的 cmd.exe 控制台窗口具有,让系统定位窗口默认情况下的属性对话框中的复选框,所有新窗口都将完全重叠,以便您立即看到最后一个打开。

  • 最后打开的新窗口将具有键盘焦点。

  • 传递给/k 的命令被立即执行,但然后输入交互式会话。


mohamed saeed's answer shows you to execute cmd.exe commands synchronously, in sequence in the current console window.

If, by contrast, you want to open multiple interactive cmd.exe sessions, asynchronously, each in its separate, new window, use cmd.exe's /k option and invoke cmd.exe via Start-Process:

# Open three new interactive cmd.exe sessions in new windows 
# and execute a sample command  in each.
'date /t', 'ver', "echo $PSHOME" | ForEach-Object {
  # Parameters -FilePath and -ArgumentList implied.
  Start-Process cmd.exe /k, $_
}

Note:

  • Unless your cmd.exe console windows have the Let the system position the window checkbox in the Properties dialog checked by default, all new windows will fully overlap, so that you'll immediately only see the last one opened.

  • The last new window opened will have the keyboard focus.

  • The commands passed to /k are instantly executed, but an interactive session is then entered.

空气里的味道 2025-01-29 08:22:06

如果您想保留纯粹的批处理,则可以使用start命令。 /k开关保持命令行打开。如果要执行命令并终止命令,则会使用 /c:

start "" %comspec% /k ping 192.168.1.1

从PowerShell中,可以使用参数列表使用start-process命令:

Start-Process cmd.exe -ArgumentList "/k ping 192.168.1.1"

If you would like to keep in purely batch, you can use the start command. The /k switch keeps the command line open. You would use /c if you want to carry out the command and terminate :

start "" %comspec% /k ping 192.168.1.1

From powershell, you can use the Start-Process command with an ArgumentList:

Start-Process cmd.exe -ArgumentList "/k ping 192.168.1.1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文