Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
}



Posted
Updated 1-Feb-17 3:17am

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.
 
Share this answer
 
Aye I see, well then I have no choice but to do it with power shell execution script.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900