在远程计算机上使用远程计算机上的psexec

时间:2014-09-11 14:00:18

标签: c# remote-access remote-server psexec

我的服务器上有一个网页,我尝试点按一下按钮,在另一台服务器上运行.exe。杀人工作正常,但我使用psexec的部分不起作用。它适用于我从本地主机(针对远程计算机)调试而不是从服务器调试远程计算机。我错过了什么?

var iu = new ImpersonateUser();
    var serverName = "serverName";
    //the process to restart
    const string processName = "FileToRun.exe";

    try
    {
        iu.Impersonate(_domain, _userName, _pass);

        ManagementPath path = new ManagementPath
        {
            Server = serverName,
            NamespacePath = "\\ROOT\\CIMV2",
            ClassName = "Win32_Process"
        };

        ManagementScope scope = new ManagementScope(path);

        //Select process that matches processname
        var query = new SelectQuery("SELECT * from Win32_process WHERE name = '" + processName + "'");

        using (var searcher = new ManagementObjectSearcher(scope, query))
        {
            foreach (ManagementObject process in searcher.Get())
            {
                //Kill the process
                process.InvokeMethod("Terminate", null); //Working
            }
        }

        Thread.Sleep(3000);

        //Start the process

        ProcessStartInfo info = new ProcessStartInfo(@"C:\PsTools"); //Directory on server where psexec.exe is
        info.FileName = @"C:\PsTools\psexec.exe"; //Direct path to psexec
        info.Arguments = @"\\" + serverName + @" -i -u " + _domain + @"\" + _userName + " -p " + _pass +
                         @" F:\Utils\" + processName; //Tried C-drive instead of F. Same result

        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;
        info.CreateNoWindow = true;
        Process p = Process.Start(info);

        lblStatusResponse.Text = "Service " + processName + " was restarted correctly.";
    }
    catch (Exception ex)
    {
        lblStatusResponse.Text = ex.ToString();
    }
    finally
    {
        iu.Undo();
    }

0 个答案:

没有答案