在 C# 中杀死远程计算机上的进程

发布于 2024-07-10 02:36:21 字数 135 浏览 7 评论 0原文

仅有助于杀死本地进程机器。 如何终止远程计算机上的进程?

This only helps kills processes on the local machine. How do I kill processes on remote machines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹沉浮 2024-07-17 02:36:21

您可以使用 wmi。 或者,如果您不介意使用外部可执行文件,请使用 pskill

You can use wmi. Or, if you don't mind using external executable, use pskill

烟雨凡馨 2024-07-17 02:36:21

我喜欢这个(类似于 Mubashar 的回答):

ManagementScope managementScope = new ManagementScope("\\\\servername\\root\\cimv2");
managementScope.Connect();
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_Process Where Name = 'processname'");
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
foreach (ManagementObject managementObject in managementObjectCollection)
{
    managementObject.InvokeMethod("Terminate", null);
}

I like this (similar to answer from Mubashar):

ManagementScope managementScope = new ManagementScope("\\\\servername\\root\\cimv2");
managementScope.Connect();
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_Process Where Name = 'processname'");
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
foreach (ManagementObject managementObject in managementObjectCollection)
{
    managementObject.InvokeMethod("Terminate", null);
}
回眸一笑 2024-07-17 02:36:21

我使用以下代码。 psKill 也是一个好方法,但有时您需要检查其他一些内容,例如在我的情况下,远程计算机正在运行同一进程的多个实例,但使用不同的命令行参数,因此以下代码对我有用。

ConnectionOptions connectoptions = new ConnectionOptions();
connectoptions.Username = string.Format(@"carpark\{0}", "domainOrWorkspace\RemoteUsername");
connectoptions.Password = "remoteComputersPasssword";

ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
scope.Options = connectoptions;

SelectQuery query = new SelectQuery("select * from Win32_Process where name = 'MYPROCESS.EXE'");

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
       ManagementObjectCollection collection = searcher.Get();

       if (collection.Count > 0)
       {
           foreach (ManagementObject mo in collection)
           {
                uint processId = (uint)mo["ProcessId"];
                string commandLine = (string) mo["CommandLine"];

                string expectedCommandLine = string.Format("MYPROCESS.EXE {0} {1}", deviceId, deviceType);

                if (commandLine != null && commandLine.ToUpper() == expectedCommandLine.ToUpper())
                {
                     mo.InvokeMethod("Terminate", null);
                     break;
                }
            }
       }
}

I use the following code. psKill is also a good way to go but sometimes you need to check the some other stuff, for example in my case remote machine was running multiple instances of same process but with different command line arguments, so following code worked for me.

ConnectionOptions connectoptions = new ConnectionOptions();
connectoptions.Username = string.Format(@"carpark\{0}", "domainOrWorkspace\RemoteUsername");
connectoptions.Password = "remoteComputersPasssword";

ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
scope.Options = connectoptions;

SelectQuery query = new SelectQuery("select * from Win32_Process where name = 'MYPROCESS.EXE'");

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
       ManagementObjectCollection collection = searcher.Get();

       if (collection.Count > 0)
       {
           foreach (ManagementObject mo in collection)
           {
                uint processId = (uint)mo["ProcessId"];
                string commandLine = (string) mo["CommandLine"];

                string expectedCommandLine = string.Format("MYPROCESS.EXE {0} {1}", deviceId, deviceType);

                if (commandLine != null && commandLine.ToUpper() == expectedCommandLine.ToUpper())
                {
                     mo.InvokeMethod("Terminate", null);
                     break;
                }
            }
       }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文