I have a remote server name
(windows), username
and password
.
Using C# .Net, I want to run a command
on the remote server and get back the console output
Is there a way to do it in C#?
I was able to run the command using WMI
with the following code (partial) but with no luck of getting the console output. I could only get back the Process ID
.
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(scope, managementPath,objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "cmd.exe /c "+ mycommand;
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Any Ideas?
You can try executing a command with PsTools. One of many features they offer is PsExec. It allows you to run a command on a remote server. It should also return the results into a console (on local PC where it was run from).
This function is what I came up with after some research. Hope it helps someone else.