在远程wmi进程上附加键盘钩子 [英] Attach keyboard hook on remote wmi process

查看:88
本文介绍了在远程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");
}








推荐答案

出于非常明显的安全原因,你不能这样做。



在遥控器上启动的过程机器以不允许用户界面向登录到远程机器的用户显示的方式启动。无法为该流程提供任何输入。如果它正在等待输入,它将一直这样做,直到远程机器重新启动。



您无法向远程进程发送任何击键。
You cannot do that for very obvious security reasons.

A process launched on a remote machine is launched in a manner that does not allow a user interface to show to the user logged into the remote machine. There is no way to give any input to that process. If it's waiting for input, it will do so until the remote machine is restarted.

There is no way for you to send any keystrokes to the remote process.


我明白了,然后我别无选择,只能使用power shell执行脚本。
Aye I see, well then I have no choice but to do it with power shell execution script.


这篇关于在远程wmi进程上附加键盘钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆