2

これは、ローカル マシン上のプロセスを強制終了するのに役立ちます。リモート マシンでプロセスを強制終了するにはどうすればよいですか?

4

3 に答える 3

10

wmiを使用できます。または、外部実行可能ファイルを使用してもかまわない場合は、pskillを使用します。

于 2008-12-07T21:37:39.930 に答える
3

私はこれが好きです(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);
}
于 2014-08-19T18:41:29.477 に答える
1

次のコードを使用します。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;
                }
            }
       }
}
于 2013-11-26T04:58:39.300 に答える