【问题标题】:Batch script find process PID [closed]批处理脚本查找进程PID [关闭]
【发布时间】:2021-04-15 08:00:55
【问题描述】:

如果它的 cmd 命令行包含非常具体的关键字,我正在开发一个杀死进程的工具。

我使用 C# 编写了一个小程序,但它在一些未安装 .NET 的旧系统上失败了。

我需要你的帮助来获得任何提示或 sn-ps 以将我的 c# 代码转换为批处理脚本。

非常感谢

public static int GetSysmonLockerProcess()
        {
            try
            {
                var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT Name, CommandLine, ProcessId, Caption, ExecutablePath FROM Win32_Process");
                var collection = searcher.Get();

                foreach (var o in collection)
                {
                    var obj = (ManagementObject)o;

                    var type = obj["Process"]?.ToString().ToLower();
                    var commandLine = obj["CommandLine"]?.ToString();
                    var procId = obj["ProcessId"]?.ToString().ToLower();
                    var name = obj["Name"]?.ToString().ToLower();

                    int.TryParse(procId, out var pid);
                    if (pid == 0)
                        continue;

                    if (string.IsNullOrEmpty(commandLine))
                        continue;

                    if (name == "cmd.exe" && type == "process" && commandLine.Contains("TA-Sysmon-deploy") && commandLine.Contains("SplunkUniversalForwarder") && (commandLine.Contains("deploy.bat") || commandLine.Contains("deploy2.bat")))
                        return pid;
                }

                return -1;
            }
            catch (Exception)
            {
                return -1;
            }
        }

【问题讨论】:

  • 您应该能够使用 WMIC 实用程序检索您需要的信息。这是一个单行批处理文件命令示例,供您测试或用作基础:@WMIC Process Where "CommandLine Like '%%TA-Sysmon-deploy%%' And CommandLine Like '%%SplunkUniversalForwarder%%' And CommandLine Like '%%deploy.bat%%' And Name='cmd.exe' Or CommandLine Like '%%TA-Sysmon-deploy%%' And CommandLine Like '%%SplunkUniversalForwarder%%' And CommandLine Like '%%deploy2.bat%%' And Name='cmd.exe'" Call Terminate。请注意,这假设您的旧系统至少为 XP Pro/Server 2003。
  • 还请注意,本网站不应该回答代码请求,或为人们在语言之间转换代码。在您编写自己的代码并编辑您的问题以显示它并解释它未能完成所需的操作之前,您的问题是题外话。

标签: c# batch-file


【解决方案1】:

这是我编写的代码

@echo off
cls
title Process_Killer
:loop
echo --------- Process_Killer ---------
echo 1: Show all Tasks
echo 2: Kill Task by PID
echo 3: Kill Task by name
echo 4: Exit
set choice=
set /p choice=Choice:
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto show_tasks
if '%choice%'=='2' goto kill_by_pid
if '%choice%'=='3' goto kill_by_name
if '%choice%'=='4' goto Exit
goto error
:show_tasks
tasklist
pause 
:kill_by_pid
set pid=
set /p pid=Enter PID:
taskkill /F /PID %pid%
goto loop
:kill_by_name
set name=
set /p name=Enter process name:
taskkill /im %name% /f
goto loop
:Exit
exit
:error
echo Error:Unidentified input
goto loop

【讨论】:

    【解决方案2】:

    作为批处理文件行,

    for /f "tokens=2" %%P in ('tasklist /v ^|findstr /b /i /L /c:"cmd.exe "^|findstr /i /L /c:"wd5"') do echo taskkill /PID %%P
    

    其中wd5 是我用作目标的字符串,这仅仅是因为我的系统上碰巧有一个cmd 进程,并以该字符串作为其参数。

    echo 只是导致建议的taskkill 命令显示在控制台上。一旦您确认该过程可靠地选择了正确的 PID,请删除上述 echo 关键字以执行 taskkill

    tasklist/v 命令生成一个 (/v) 当前任务的详细列表。第一个findstr 查找那些/b/L 文字字符串/c: 常量字符串开头的行(因为它故意包含空格)“在引号中” /i -case -不敏感。

    下一个findstr 选择包含关键字wd5 的剩余行。

    ^ 转义管道以告知 cmd'single-quoted command to be executed' 的一部分,而不是封闭的 for

    for 处理来自'quoted command' 的任何行,使用默认分隔符(包括空格)对其进行标记,并将第二个标记分配给使用的元变量%%P作为DO的命令。

    所以 - 不需要 C# 异端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2015-06-30
      相关资源
      最近更新 更多