如何检查进程是否在另一台PC上运行并使用Python终止它

2024-09-29 23:23:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过Python代码检查并关闭另一台PC上运行的程序。 要关闭程序的PC通过LAN连接

我已经在C#中有了这样做的代码。但我不知道它在Python中的等效方式

string processName = "some_program.exe";
ManagementScope scope;

ConnectionOptions connectoptions = new ConnectionOptions();
connectoptions.Impersonation = ImpersonationLevel.Impersonate;
connectoptions.EnablePrivileges = true;

scope = new ManagementScope(@"\\" + RemotePCIpAddress + @"\root\cimv2", connectoptions);

// WMI query
var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");

using (var searcher = new ManagementObjectSearcher(scope, query))
{
    foreach (ManagementObject process in searcher.Get()) // this is the fixed line
    {
        print("Killing Process : " + processName);
        process.InvokeMethod("Terminate", null);
    }
}

我希望python中的程序检查进程是否在另一台PC上运行并终止它


Tags: 代码程序newvarqueryprocessscopepc

热门问题