简体   繁体   中英

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. 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. "say hello world".

For anyone who wanted to achieve this i did it using the following method.

Save as anything.BAT or anything.CMD

Change the following to set a specific ProcessID number you are targeting.

set 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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