Execute C# code on remote machine

a 夏天 提交于 2020-01-05 19:45:07

问题


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.


回答1:


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.




回答2:


Just an idea, what about .NET remoting?

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




回答3:


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



来源:https://stackoverflow.com/questions/30003408/execute-c-sharp-code-on-remote-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!