Hi All,
I've got this problem. I'm trying to write a method that will (Using WMI) start a process on a remote computer. This remote process runs an application that should open a command window and send the keys to start a program in that command window. The command window then needs to remain open for that user.
The problem is: If I execute the remote application while actually logged into that computer (not programmatically) it works fine. But, if I execute it via the WMI calls below it does not visibly bring up the command window and does not start the program the send keys is supposed to start.

The method in the local program is:

private bool ProcStarter(ManagementScope scope)
    {
      ManagementPath ProcessMgmtpath = new ManagementPath("Win32_Process");
      ManagementClass processClass = new ManagementClass(scope, ProcessMgmtpath, null);
      ManagementPath ProcessStartupMgmtpath = new ManagementPath("Win32_ProcessStartup");
      ManagementClass processStartupClass = new ManagementClass(scope, ProcessStartupMgmtpath, null);
      processStartupClass.SetPropertyValue("ShowWindow", 1);
      ManagementBaseObject inParms = processClass.GetMethodParameters("Create");
      inParms["CommandLine"] = @"C:\PATS\Bin\StartInWindow.exe";
      inParms["ProcessStartupInformation"] = processStartupClass;
      ManagementBaseObject ReturnObj = processClass.InvokeMethod("Create", inParms, null);
      Trace.WriteLine("Proc start return results:");
      Trace.WriteLine("Return Value: " + ReturnObj["returnValue"]);
      Trace.WriteLine("Process ID: " + ReturnObj["processId"]);
      return (ReturnObj["returnValue"].ToString() == "0");
    }

On the remote computer:

class StartInWindow
  {
    [DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int lWindowHandle);
    static void Main(string[] args)
    {
      System.Diagnostics.ProcessStartInfo psi = new ProcessStartInfo();
      psi.WorkingDirectory = "c:\\PATS\\Bin";
      psi.FileName = "cmd.exe";
      Process p = Process.Start(psi);
      // wait for things to settle down a bit...much longer than we really need...
      System.Threading.Thread.Sleep(5000);
      // now fire up scripttrigger in the command window by sending the (hopefully) foremost window some keys.
      int x = (int)p.MainWindowHandle;
      //Console.WriteLine("set cmd to foreground...");
      SetForegroundWindow(x);
      System.Threading.Thread.Sleep(1000);
      //Console.WriteLine("sendkeys...");
      SendKeys.SendWait("ScriptTrigger.exe~");
      System.Threading.Thread.Sleep(1000);
    }
  }

Can anyone shine any light on this? Is it possible, or are securities holding me back?
Thanks!

Hi,
Can anyone please confirm that a program can (or cannot) be started in a command window on a remote computer? My manager says it should be doable, but everything I've read says it cannot be done.
Just looking for a little guidance....
Thanks!

Can't get it to work with PsExec either... :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.