使用批处理文件将命令发送到CMD.exe

时间:2016-12-12 14:27:23

标签: batch-file cmd windows-server-2012

您好我正在尝试将命令“shutdown”发送到一个名为Administrator:Transaction Server的已打开命令提示符,使用BAT文件,但是当我运行BAT文件时没有任何反应。

我关注了这个问题 - How to execute a command in already open CMD window using VBScirpt

有谁知道为什么?

这是我的代码:

:: s
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:115 /lparam:1
:: h
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:104 /lparam:1
:: u
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:117 /lparam:1
:: t
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:116 /lparam:1
:: d
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:100 /lparam:1
:: o
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:111 /lparam:1
:: w
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:119 /lparam:1
:: n
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:110 /lparam:1
:: ENTER
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:13 /lparam:1

:: ENTER
SendMessage-1.1.2.exe /windowtitle:"Test Server" /message:WM_CHAR /wparam:13 /lparam:1

:: wait for some time (~3 sec) till the program shuts down, if necessary
PING -n 4 localhost 1>NUL

pause
...

1 个答案:

答案 0 :(得分:1)

我不知道您正在使用哪种发送消息。我下载了这个 来自Dostips.com的SendMessage.exe: Access to advanced Windows features作者:aacini

使用此批处理,我设法在标题为Test-Msg的另一个cmd窗口中运行关闭:

@ECHO OFF

Set /A WM_CHAR=0x0102
Set Msg=%WM_CHAR%

Set Win="Test-Msg"
::          s   h   u   t   d   o   w   n   ret
For %%A in (115 104 117 116 100 111 119 110 13) Do (
echo SendMessage.exe %Win% %Msg% %%A 1
     SendMessage.exe %Win% %Msg% %%A 1
)
Pause
SendMessage.exe %Win% %Msg% 13 1