简体   繁体   English

C#从远程客户端上的远程服务器运行可执行文件

[英]C# run executable from remote server on remote client

I have the following function for connecting to a remote client machine and running an executable on it. 我具有以下功能,用于连接到远程客户端计算机并在其上运行可执行文件。

I copied it from a response on this site, but don't have the link anymore, so I'm not sure who to give credit to. 我是从此站点上的回复中复制它的,但是现在没有链接了,所以我不确定应该归功于谁。

    public static void ConnectToRemoteClient(string client_machine, string user, string password, string target_exe )
    {
        var connection = new ConnectionOptions();
        // The '.\' is for a local user on the remote machine
        // or 'mydomain\user' for a domain user
        connection.Username = user;
        connection.Password = password;


        object[] theProcessToRun = { target_exe };

        var wmiScope = new ManagementScope($@"\\{client_machine}\root\cimv2", connection);

        wmiScope.Connect();

        using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
        {
            managementClass.InvokeMethod("Create", theProcessToRun );
        }   
    }

It is called using the following syntax: 使用以下语法调用它:

    string exe = string.Format("taskkill.exe {0} {1}", @"/F", @"/PID 8704");
    ConnectToRemoteClient("ClientMachine", @"Domain\Username", @"password", exe);

It works just fine for executables that exist on the remote client machine. 对于远程客户端计算机上存在的可执行文件,它工作得很好。

However, I want to call an executable from a server, and run it on that remote client machine. 但是,我想从服务器调用可执行文件,然后在该远程客户端计算机上运行它。

Not sure how best to approach this. 不知道如何最好地解决这个问题。 I tried feeding it the following: 我尝试将其喂食以下内容:

    ConnectToRemoteClient("ClientMachine", @"Domain\User", @"password", @"\\ServerName\MyDir\Myfile.exe");

But it never initiated the executable on the machine. 但是它从未在计算机上启动可执行文件。 No error messages. 没有错误讯息。

The reason I want to do this, is to save me from having to copy the large executable and supporting files to each client, but rather just run it from the server depot on each remote client. 我这样做的原因是,使我不必将大型可执行文件和支持文件复制到每个客户端,而只是从每个远程客户端上的服务器仓库运行它。

Do I have to call a CMD window and feed it the \\\\ServerName\\MyDir\\Myfile.exe in order to get it to work properly? 我是否必须调用CMD窗口并将其输入\\\\ ServerName \\ MyDir \\ Myfile.exe,才能使其正常工作? or is there a way I can make this work? 或者有什么方法可以使这项工作?

You are connecting to the remote machine using the passed credentials, but that only establishes your rights to open WMI. 您正在使用传递的凭据连接到远程计算机,但这仅建立您打开WMI的权限。 The command you then pass to WMI to execute is not running as the credentials you pass in, but under the LocalSystem account credentials. 然后传递给WMI来执行的命令不是作为传递的凭据运行,而是在LocalSystem帐户凭据下运行。

LocalSystem does not have access to the network share. LocalSystem无权访问网络共享。

To do this you need to remotely execute PSEXEC ( https://ss64.com/nt/psexec.html ) which allows you to pass the parameters to launch the application as. 为此,您需要远程执行PSEXEC( https://ss64.com/nt/psexec.html ),它允许您传递参数以启动应用程序。 PSEXEC runs as LocalSystem but allows you to pass credentials to use when it launches your designated application. PSEXEC作为LocalSystem运行,但是允许您传递凭据以在启动指定的应用程序时使用。 The launched program will then impersonate the user you pass in, and will have access to the network share. 然后,启动的程序将模拟您传递的用户,并将有权访问网络共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM