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.

4
  • Any reason why you can't just use winrs to run the remote batch file? EG winrs -r:(remote sserver hostname) c:\recycle.bat Commented Jul 27, 2015 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. Commented Jul 27, 2015 at 17:47
  • 1
    Ah. So if it a gui based thing in C# then see this thread: stackoverflow.com/questions/428276/… but if you want it to work through powershell, then try this link: stackoverflow.com/questions/9535515/… Commented Jul 27, 2015 at 18:04
  • Thank you for your Reply Tim, It's much appreciated! I'll look into both methods now. Commented Jul 27, 2015 at 18:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, WMI would work too, but the code to create a shell object to run winrs (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.
I agree with you. But it was mentioned as GUI so I suggested this option
It seems the link you provided is outdated. May you provide another example to follow?
Don't judge that by UI. You can customize your UI and the code for core logic is available.
Thanks, But I cannot find the "baileysoft.WMI.process" reference which is being used in the article.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.