1

I'm tasked with automating an internal process. This process involves first being logged on a remote server (A). From server A, a user would connect to Remote Server (B).

Once authenticated onto Server B, the C# application needs to run a batch file

I've used some sample code form a post on CodeProject to make all the remote desktop connections through some GUI and it's working without issue. The codes uses the ActiveX MSTSC Library.

I then sue this block of code, hoping to start the Batch File:

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {
            //PowerShell ps = PowerShell.Create();
            //ps.AddCommand("Start-Process");
            //ps.AddArgument("/c c:\\Recycle.bat");
            //ps.Invoke();

            Process p = new Process();
            p.StartInfo.FileName = "C://Recycle.bat";
            p.StartInfo.CreateNoWindow = false;
            p.Start();
        }

        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

I've tried using the PowerShell class, as well as the System.Diagnostics.Process objects. The PS object yeilds "no file found", where the process objects executes the "Recycle.bat" script on my local computer, not the remote server.

How would you attach the Process object to the remote server and not my local PC?

Thank you for your help.

Stephen Sugumar
  • 545
  • 3
  • 9
  • 35
  • Any reason why you can't just use winrs to run the remote batch file? EG `winrs -r:(remote sserver hostname) c:\recycle.bat` – Tim Jul 27 '15 at 17:45
  • This application is being developed as an easy GUI to be used by an internal team. Eventually, the application should have a checklist of servers, where the users ticks off the servers to "recycle" and clicks run. – Stephen Sugumar Jul 27 '15 at 17:47
  • 1
    Ah. So if it a gui based thing in C# then see this thread: http://stackoverflow.com/questions/428276/how-to-execute-a-command-in-a-remote-computer but if you want it to work through powershell, then try this link: http://stackoverflow.com/questions/9535515/powershell-execute-remote-exe-with-command-line-arguments-on-remote-computer – Tim Jul 27 '15 at 18:04
  • Thank you for your Reply Tim, It's much appreciated! I'll look into both methods now. – Stephen Sugumar Jul 27 '15 at 18:05

1 Answers1

1

There is something called WMI. Try the following URL http://www.codeproject.com/Articles/18146/How-To-Almost-Everything-In-WMI-via-C-Part-Proce

  • Yes, WMI would work too, but the code to create a shell object to run winrs (http://stackoverflow.com/questions/1469764/run-command-prompt-commands) is much lighter (3 lines) than the code to run a remote process through WMI (~20?). However, it may be more aesthetically pleasing to use WMI since you won't have a black dos window popping up briefly. – Tim Jul 28 '15 at 14:42
  • 1
    I agree with you. But it was mentioned as GUI so I suggested this option – Deepak Rajappan Jul 28 '15 at 18:07
  • It seems the link you provided is outdated. May you provide another example to follow? – Stephen Sugumar Jul 30 '15 at 13:04
  • Don't judge that by UI. You can customize your UI and the code for core logic is available. – Deepak Rajappan Jul 30 '15 at 16:22
  • Thanks, But I cannot find the "baileysoft.WMI.process" reference which is being used in the article. – Stephen Sugumar Jul 31 '15 at 12:21
  • Read the last part of article. Bailey soft is not a reference – Deepak Rajappan Aug 15 '15 at 04:04