本文介绍了在远程wmi进程上附加键盘钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi I have a question about interctivity while using WMI in c#, I am running command on my remote desktop using wmi for example:
cmd /c C:\test.exe
Now while doing that process asks to press any key to continue.
I had an idea that command would dump result to some sort of log file so command wold look like this cmd /c C:\test.exe > log.txt and then event watcher would check log in while loop for pause fraze, but then I do not know how to create press key process /keyboard hook and attach it to my command process on remote desktop.
Is there an easier way to catch that message and press key when the process asks?
我尝试过:
What I have tried:
private void ExecuteCommand(string command)
{
ConnectionOptions connection = new ConnectionOptions { Username = UserName, Password = Password };
this.wmiScope = new ManagementScope(string.Format(@"\\{0}\root\CIMV2", IPAddress), connection);
try
{
this.wmiScope.Connect();
}
catch (Exception e)
{
var exceptionMessage = string.Format("Management Connect to remote machine {0} failed with the following error {1}", this.dutConfig.IPAddress, e.Message);
throw new Exception(exceptionMessage);
}
this.Logger.AddMessage("Start wmi process: {0}", command);
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
using (ManagementClass processClass = new ManagementClass(this.wmiScope, managementPath, objectGetOptions))
{
using (ManagementBaseObject inParams = processClass.GetMethodParameters("Create"))
{
inParams["CommandLine"] = command;
using (ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null))
{
if (outParams != null && (uint)outParams["returnValue"] != 0)
{
throw new Exception(string.Format("Error while starting process {0} creation returned an exit code of {1}", command, outParams["returnValue"]));
}
if (outParams != null)
{
this.processId = (uint)outParams["processId"];
this.ExitCode = Convert.ToUInt16(outParams["returnValue"]);
}
}
}
}
this.Logger.AddMessage("Start monitoring event for wmi process: {0}, with id: {1}", command, this.processId);
this.EventWatcher();
this.Logger.AddMessage("Wmi Command has finished");
}
推荐答案
这篇关于在远程wmi进程上附加键盘钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!