如何发送多个语句到同一个.cmd实例?

如何发送多个语句到同一个.cmd实例?

问题描述:

如何发送多个命令到.cmd脚本的同一个实例?从cmd powershell执行此操作是可以接受的。

How can I send multiple commands to the same instance of a .cmd script? Doing so from either cmd or powershell is acceptable.

我不是试图在单个连接语句中发送多个命令 - 字符限制可能是一个非常大的脚本的因素。相反,我想将多个命令作为单独的语句发送到正在运行的.cmd脚本。当我说.cmd,我不是指cmd.exe - 我的意思是一个自定义shell脚本,保存到磁盘与 .cmd extension。

I'm not trying to to send multiple commands in a single concatenated statement-- character-limits could be a factor with very large scripts. Rather, I want to send multiple commands, as separate statements, to a running .cmd script. When I say ".cmd", I do not mean "cmd.exe"-- I mean a custom shell script, saved to disk with a .cmd extension.

具体来说,此帖子演示如何创建powershell4.cmd脚本这将启动.Net 4.0 PowerShell。我想向该powershell发送一系列命令。

Specifically, this post shows how to create a powershell4.cmd script which launches a .Net 4.0 PowerShell. I want to send a sequence of commands to that powershell.

例如,是否可以获取该新shell的句柄,以向其发送其他命令?

For example, is it possible to get a handle to that new shell, in order to send additional commands to it?

另一种方法是将保存的命令保存到文件中(例如 test.in )然后使用如下命令:

Another approach might be to put the save the commands you want to send in a file (e.g. test.in) and then use a command like this:

Start-Process -RedirectStandardInput test.in cmd 

考虑添加 -Wait -NoNewWindow 。你需要实验一些,看看这如何与批处理脚本交互。

Consider adding -Wait and -NoNewWindow. You would need to experiment some to see how this interacts with a batch script.

我不相信PowerShell本身支持重定向到管道,但它可以通过调用.NET运行时来实现。

I don't believe PowerShell natively supports redirecting to a pipe, but it might be doable by calling the .NET runtime.