使用C#检查进程是否在远程系统上运行

问题描述 投票:9回答:6

我试图检查进程是否在远程系统上运行。我使用以下代码:

string procSearc = "notepad";
string remoteSystem = "remoteSystemName";

Process[] proce = System.Diagnostics.Process.GetProcessesByName(procSearch, remoteSystem);

但是,当我尝试运行代码时,我收到以下错误:“无法连接到远程计算机。”

我可以使用以下命令运行pslist:C:> pslist \ remoteSystemName所以我知道可以获取我需要的信息,但我需要在代码中。

另一种可能性是将pslist集成到C#中并搜索列表以查看该进程是否存在,但我还没有找到有关如何执行此操作的信息。

c# .net system.diagnostics
6个回答
8
投票

使用System.ServiceProcess.ServiceController类进行服务。您可以使用Status检查它是否正在运行以及Stop()Start()来控制它。

ServiceController sc = new ServiceController();
sc.MachineName = remoteSystem;
sc.ServiceName = procSearc;

if (sc.Status.Equals(ServiceControllerStatus.Running))
{
   sc.Stop();
}
else
{
   sc.Start();
}

4
投票

以下是我为使其工作而采取的措施:

首先,我添加了对System.ServiceProcess的引用,并添加了:using System.ServiceProcess;

string remoteSystem = "remoteSystemName";
string procSearch = "notepad";            
Process[] proc = System.Diagnostics.Process.GetProcessesByName(procSearch, remoteSystem);

   if (proc.Length > 0)

   {
        Console.WriteLine("Able to find: " + proc[0]);
   }
   else
   {
        Console.WriteLine("Unable to find: " + procSearch);
   }

1
投票

内部异常是否说“访问被拒绝”?

类似的问题可能有所帮助,它提到需要在Performance Monitor Users组中。

GetProcessesByName() and Windows Server 2003 scheduled task


0
投票

我想在运行应用程序时,我会得到一个异常窗口,因为我的代码中没有处理异常......

我在堆栈跟踪中看到了原因:访问被拒绝。原因是运行调用.NET方法以获取进程列表的程序的用户不是远程计算机的“性能监视器用户”组的一部分。

之后,我得到另一个异常,说性能监控服务没有在远程计算机上运行。所以我在远程计算机上启动了相应的服务,并且它工作了!

这是使用Windows 7客户端尝试获取Windows 2008 Server的进程列表。


0
投票

杀死远程进程 我发现Process.Kill()方法在设置Process.MachineName时不起作用,所以这里有一个远程终止进程的解决方案,希望它能帮助其他人。 创建方法的扩展方法:KillRemoteProcess

public static class ProcessExtensions
{
    public static void KillRemoteProcess(this Process p, string user, string password)
    {
        new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "TaskKill.exe",
                Arguments = string.Format("/pid {0} /s {1} /u {2} /p {3}", p.Id, p.MachineName, user, password),
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true
            }
        }.Start();
    }
}

并且当然是找到进程并使用KillRemoteProcess的方法

 public static void KillProcessesRemote()
    {
        string targetProcessName = "myProcess"; //Do not put 'process.exe' here just 'process'
        string targetMachine = "remotMachine"; //Target machine
        string username = "myUser"; //Username
        string password = "myPassword"; //Password

        Parallel.ForEach<Process>( //Kill all processes found
            source: System.Diagnostics.Process.GetProcessesByName(targetProcessName, targetMachine),
            body: process => {
                process.KillRemoteProcess(username, password);
            });
    }

0
投票

您可以尝试模拟有权访问远程服务器的用户。

https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsimpersonationcontext?redirectedfrom=MSDN&view=netframework-4.7.2

模拟后,您将不再遇到错误。此外,您必须确保域之间存在信任,否则模拟将无效。

LogonUser works only for my domain

© www.soinside.com 2019 - 2024. All rights reserved.