【发布时间】: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