2

i have a piece of code which i want to execute on remote computer. I am not getting how to do that. Like if i want to execute command given below on remote machine how to do that.

 Process[] processlist = Process.GetProcesses();

it is just an example, like i want to execute whole code on remote machine with libraries.

if you are going to answer for getting processlist as

SelectQuery selectQuery = new SelectQuery("Select * from Win32_Process");
using(ManagementObjectSearcher searcher =new ManagementObjectSearcher(manScope, selectQuery))

i already know that , please save it. is there any way to execute whole code after connecting to scope.

leppie
  • 115,091
  • 17
  • 196
  • 297
Kaustubh_Kharche
  • 725
  • 3
  • 13
  • 34
  • just that I get it right: you want to sent some code-piece to some other machine, let it execute there and get the result back? And all this without having to set up something on the remote machine? ... So basically you are asking of how to setup a bot-net? – Random Dev May 02 '15 at 14:27
  • @CarstenKönig actually the code is same which i want to send. just parameters will differ. but you got that right. i want that code to execute on remote machine. like i want to execuete uint dwSessionId1 = WTSGetActiveConsoleSessionId() and WTSQueryUserToken(dwSessionId1, ref hUserToken).. on remote machine. when i tried it from my local machine it returned me my local machines attributes. – Kaustubh_Kharche May 02 '15 at 14:33
  • Your question is really unclear. Are you saying you have all your code on machine A and you want to say, "OK machine B, run this code that I have on machine A". You can't do that. You might try something like psexec https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx. – aquinas May 02 '15 at 14:33
  • @aquinas i dont want to use external tools. and i have explained in above comment what i want to do. – Kaustubh_Kharche May 02 '15 at 14:36
  • 2
    are you aware that if you manage this you will need some administrative rights on the remote machine? Usually you just write some kind of server-app that you can call and that the administrator will have to install and run on the target system – Random Dev May 02 '15 at 14:40
  • @kaustubh93, this seems like an XY problem to me: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378. Instead of asking about executing code on a remote machine, can you explain what the *end* goal is you are trying to accomplish? – aquinas May 02 '15 at 14:48
  • @aquinas i want to launch a application on remote machine with its GUI in currently logged in desktop session. – Kaustubh_Kharche May 02 '15 at 14:54
  • @CarstenKönig while remotely logging in I am using username password of administrator account of that remote machine..will that do..?? – Kaustubh_Kharche May 02 '15 at 14:55
  • How about just using `at` / `schtasks` ? https://support.microsoft.com/en-us/kb/313565?wa=wsignin1.0 – aquinas May 02 '15 at 14:57
  • @aquinas i thought the same. but problem is, schtasks will work on vista and windows 7 . for windows 8 and server 2012 it wont work. – Kaustubh_Kharche May 02 '15 at 15:02
  • That's not true. schtasks works on Windows 8 and 2012. – aquinas May 02 '15 at 15:08
  • @aquinas sorry i didnt knew that. read somewhere that schtask dont work work with it so they added jt.exe. it it works then well and good. btw i need an favour. do you know how to use schtask in powershell to schedule a task. – Kaustubh_Kharche May 02 '15 at 15:13
  • @aquinas will you please take a look at http://stackoverflow.com/questions/30007829/scheduling-task-in-windows-2008-with-powershell – Kaustubh_Kharche May 04 '15 at 17:36

4 Answers4

1

for getting list of process on remote machine just enter name of remote machine in brackests..

      Process[] processlist = Process.GetProcesses(Remotemachine);

for reference: https://msdn.microsoft.com/en-us/library/1f3ys1f9%28v=vs.110%29.aspx

Kaustubh_Kharche
  • 725
  • 3
  • 13
  • 34
0

There's no overload for GetProcesses() on a remote machine, but if you're just looking for a particular process name, you could try the GetProcessesByName(string, string) overload, which allows you to pass a machine name as the second parameter.

See MSDN for more information: https://msdn.microsoft.com/en-us/library/725c3z81(v=vs.110).aspx

Also, please note that you are not actually executing your command on a remote computer, but rather making a request to a remote computer. In order to do that, you would want to look into WCF (Windows Communication Foundation; start here: https://msdn.microsoft.com/en-us/library/ms731082%28v=vs.110%29.aspx). You could run a service on the remote computer and have a client on your local computer consume that service. The client might send a message like "Get me all the processes!" and the service would return a list of processes after running Process.GetProcesses() on the remote machine.

If you're asking about a way to execute arbitrary code on a remote machine without any special tooling or libraries, there is none (or rather, there are at times but they're considered major security vulnerabilities and get patched when found).

Alternatively, it's possible to make use of the Remote Desktop Protocol (RDP) to do what you're trying to achieve. It's possible to make use of this programatically, but you might have a hard time limiting it to just one application.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
  • it was an example. actually i want to exeute code on remote machine with different parameters. like i want to execute uint dwSessionId1 = WTSGetActiveConsoleSessionId() and WTSQueryUserToken(dwSessionId1, ref hUserToken) on remote machine. when i tried it from my local machine it returned me my local machines attributes. – Kaustubh_Kharche May 02 '15 at 14:40
  • Right. You'd need to write a service that executes that code running on the remote machine and then have that service return the values to your local machine. – Dan Field May 02 '15 at 14:41
  • okay. but what if when i try my code from a Client and machine is not running that service at that time. secondly is there any other way without running that service. and also initially do i have to go to each computer and set up that service. or i can do set up service remotely.??? – Kaustubh_Kharche May 02 '15 at 14:45
  • You'd have to set up the service on that machine. If you're a network administrator and your network has facilities to do so, you might be able to remotely install software (or require users to do so). But this is sounding more and more like you're trying to create security problems... – Dan Field May 02 '15 at 14:50
  • yes i know. actually i want to launch an application on remote machine with its GUI in currently logged in users desktop. i dont want to use any external tools. – Kaustubh_Kharche May 02 '15 at 15:00
  • Then look into the Remote Desktop Protocol. – Dan Field May 02 '15 at 15:01
0

Just an idea, what about .NET remoting?

https://msdn.microsoft.com/library/kwdt6w2k%28v=VS.71%29.aspx?f=255&MSPPError=-2147217396

Vincent F
  • 371
  • 3
  • 17
0

Some possibilities:

  1. use PowerShell to run remote commands: link
Enter-PSSession -ComputerName your-computer-name -Credential your-user-name
Set-Location "C:\your\code\location.exe"
.\location.exe
  1. the NuGet package SSH.net: link
//using Renci.SshNet; //Make sure the Nuget package is install and compatible
using (var client = new SshClient("your-computer-name", "your-user-name", "your-password")) 
{
    client.Connect();
    client.RunCommand("cd .\your\code\");
    SshCommand sc = client.RunCommand("location.exe");
    MessageBox.Show(sc.Error);
    MessageBox.Show(sc.Result);
    client.Disconnect();
}
  1. initiate remote control via c#: link
Process process = new Process();
process.StartInfo = new ProcessStartInfo(@"cmd.exe", "/C mstsc /v:\"your-computer-name\"");
process.Start();
public static string DoProcess(string cmd, string argv)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = cmd;
            p.StartInfo.Arguments = $" {argv}";
            p.StartInfo.CreateNoWindow = true;
            //p.StartInfo.Verb = "RunAs";
            p.Start();
            p.WaitForExit();
            string output = p.StandardOutput.ReadToEnd();
            p.Dispose();

            return output;
        }
xldk
  • 11
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30316979) – Bakar Nov 12 '21 at 11:03