简体   繁体   English

如何通过命令行或批处理将文本粘贴到特定进程ID的控制台窗口中?

[英]How do I paste text to a console window of an specific process ID via command line or with batch?

So i have a specific process id (PID) works the same as a command prompt window. 因此,我有一个特定的进程ID(PID)与命令提示符窗口相同。 I want to send a line of text to it via a batch file. 我想通过批处理文件向其发送一行文本。

The process running will be "srcds.exe" i want my batch file to paste text into the window of that process that would for example say the following. 运行的进程将是“ srcds.exe”,我希望我的批处理文件将文本粘贴到该进程的窗口中,例如说以下内容。 "say hello world". “打个招呼世界”。

For anyone who wanted to achieve this i did it using the following method. 对于任何想要实现这一目标的人,我都使用以下方法来实现。

Save as anything.BAT or anything.CMD 另存为BAT其他任何内容

Change the following to set a specific ProcessID number you are targeting. 更改以下内容以设置您要定位的特定ProcessID号。

set pid=5540 设置pid = 5540

@echo off
set pid=5540
echo Option Explicit >temp.vbs
echo Dim Shell, WMI, wql, process >>temp.vbs
echo Set Shell = CreateObject("WScript.Shell") >>temp.vbs
echo Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") >>temp.vbs
echo wql = "SELECT ProcessId FROM Win32_Process WHERE ProcessId = '%pid%'" >>temp.vbs
echo For Each process In WMI.ExecQuery(wql) >>temp.vbs
echo Shell.AppActivate process.ProcessId >>temp.vbs
echo Shell.SendKeys "say hello world!" >>temp.vbs
echo Shell.SendKeys "{ENTER}" >>temp.vbs
echo Next >>temp.vbs
cscript //nologo temp.vbs & del temp.vbs
echo done
pause

The plain anything.VBS (Visual basic script) format is as follows. 普通的任何内容。VBS (Visual Basic脚本)格式如下。

Option Explicit
Dim Shell, WMI, wql, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
wql = "SELECT ProcessId FROM Win32_Process WHERE ProcessId = '5540'"
For Each process In WMI.ExecQuery(wql)
Shell.AppActivate process.ProcessId
Shell.SendKeys "say Hello World!"
Shell.SendKeys "{ENTER}"
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM