使用TDOSCommand调用Powershell脚本对进程进行操作

列出当前运行的进程:

Delphi 复制代码
var
  PowerShellPath, ScriptPath, CommandLine: string;
begin
  Memo6.Clear;
  PowerShellPath := 'powershell.exe '; // 假设 PowerShell 可执行文件在系统环境变量中
  // 构造命令行参数
  CommandLine := 'Get-Process | Select-Object Name,Id';
  // 设置命令行参数
  DosCommand2.CommandLine := PowerShellPath + CommandLine;
  // 启动进程
  DosCommand2.Execute;

按照进程的id列出详细信息

Delphi 复制代码
var
  PowerShellPath, ScriptPath, CommandLine: string;
begin

  PowerShellPath := 'powershell.exe '; // 假设 PowerShell 可执行文件在系统环境变量中
  // 构造命令行参数
  CommandLine := ' Get-Process | Where-Object { $_.id -eq ' + #39 + self.Edit4.Text + #39 + '}';
  // 设置命令行参数
  DosCommand2.CommandLine := PowerShellPath + CommandLine;
  // 启动进程
  DosCommand2.Execute;
end;

按照进程的name列出详细信息

Delphi 复制代码
var
  PowerShellPath, ScriptPath, CommandLine: string;
begin

  PowerShellPath := 'powershell.exe '; // 假设 PowerShell 可执行文件在系统环境变量中
  // 构造命令行参数
  CommandLine := ' Get-Process | Where-Object { $_.name -eq ' + #39 + self.Edit5.Text + #39 + '}';
  // 设置命令行参数
  DosCommand2.CommandLine := PowerShellPath + CommandLine;
  // 启动进程
  DosCommand2.Execute;
end;

按照id来kill进程

Delphi 复制代码
var
  PowerShellPath, ScriptPath, CommandLine: string;
begin

  PowerShellPath := 'powershell.exe '; // 假设 PowerShell 可执行文件在系统环境变量中
  // 构造命令行参数
  CommandLine := ' Stop-Process -Id  ' + #39 + self.Edit6.Text + #39 + ' -ErrorAction Stop';
  // 设置命令行参数
  DosCommand2.CommandLine := PowerShellPath + CommandLine;
  // 启动进程
  DosCommand2.Execute;
end;

将返回的结果加载到stringgrid中

Delphi 复制代码
var
  ProcessList: TStringList;
  ProcessInfo: TStringList;
  i, j: Integer;
begin


// 获取命令行输出
  ProcessList := TStringList.Create;
  ProcessInfo := TStringList.Create;
  try
    ProcessList.Text := Memo6.Text;

    // 在表格中显示进程列表
    StringGrid1.RowCount := ProcessList.Count + 1;
    StringGrid1.Cells[0, 0] := '进程名称';
    StringGrid1.Cells[1, 0] := 'PID';

    for i := 0 to ProcessList.Count - 1 do
    begin
      ProcessInfo.CommaText := ProcessList[i];
      for j := 0 to ProcessInfo.Count - 1 do
        StringGrid1.Cells[j + 1, i + 1] := ProcessInfo[j];
    end;
  finally
    ProcessList.Free;
    ProcessInfo.Free;
  end;
end;

选择行

Delphi 复制代码
procedure TForm1.StringGrid1FixedCellClick(Sender: TObject; ACol, ARow: Integer);
begin
  StringGrid1.Options := StringGrid1.Options + [goRowSelect];

// 选择第2行(索引从0开始)
  StringGrid1.Row := ARow;  // 如果要选择多行,可以使用StringGrid1.Row属性的范围来选择多个连续行

// 可选:将焦点设置到StringGrid以确保选择可见
  StringGrid1.SetFocus;
end;

选择单元格

Delphi 复制代码
procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ACol: Integer;
  ARow: Integer;
begin
  // 获取点击位置的列索引
  ACol := StringGrid1.MouseCoord(X, Y).X;
  ARow := StringGrid1.MouseCoord(X, Y).Y;
  // 判断是否点击了第一列(索引为0)
  if ACol = 0 then
  begin
    // 在这里执行第一列被点击时的操作
    StringGrid1.Options := StringGrid1.Options + [goRowSelect];

// 选择第2行(索引从0开始)
    StringGrid1.Row := ARow;  // 如果要选择多行,可以使用StringGrid1.Row属性的范围来选择多个连续行

// 可选:将焦点设置到StringGrid以确保选择可见
    StringGrid1.SetFocus;
  end;
  if ACol <> 0 then
  begin
    // 在这里执行第一列被点击时的操作
    StringGrid1.Options := StringGrid1.Options - [goRowSelect];

 选择第2行(索引从0开始)
//    StringGrid1.Row := ARow;  // 如果要选择多行,可以使用StringGrid1.Row属性的范围来选择多个连续行

// 可选:将焦点设置到StringGrid以确保选择可见
    StringGrid1.SetFocus;
  end;
end;

鼠标点击时将name和id分别赋予相应的输入框,为下一步操作做准备。

Delphi 复制代码
procedure TForm1.StringGrid1Click(Sender: TObject);
var
  SelectedRow, ColIndex: Integer;
  CellValue: Integer;
begin
  SelectedRow := StringGrid1.Row; // 获取选定的行索引

  // 遍历选定行的所有单元格
  for ColIndex := 0 to StringGrid1.ColCount - 1 do
  begin
    // 检查单元格的内容是否为数字
    if TryStrToInt(StringGrid1.Cells[ColIndex, SelectedRow], CellValue) then
    begin
      Edit4.Text := CellValue.ToString;  // 将数字内容显示在Edit组件中
//      Edit6.Text := CellValue.ToString;  // 将数字内容显示在Edit组件中
      Edit6.Text := CellValue.ToString;  // 将数字内容显示在Edit组件中
      Exit;  // 找到数字内容后退出循环
    end;
    Edit5.Text := StringGrid1.Cells[ColIndex, SelectedRow]
  end;

  // 如果未找到数字内容,将Edit组件清空
  Edit6.Text := '';

end;

结果如下

相关推荐
艾莉丝努力练剑10 小时前
【Linux:文件 + 进程】进程间通信进阶(1)
linux·运维·服务器·网络·c++·人工智能·进程
果果燕1 天前
进程间通信(IPC)- 管道专题学习笔记
进程·管道
夫唯不争,故无尤也4 天前
curl与Invoke-RestMethod核心区别
后端·fastapi·powershell·curl
顶点多余5 天前
进程:计算机世界的执行单元
linux·运维·服务器·进程
bu_shuo6 天前
Windows电脑使用VSCode远程控制Windows主机方法记录
windows·vscode·ssh·powershell
_OP_CHEN6 天前
【Linux系统编程】(四十五)线程池基础:日志系统设计与策略模式的优雅落地
linux·操作系统·线程池·进程·策略模式·c/c++·日志系统
_OP_CHEN6 天前
【Linux系统编程】(四十七)线程安全与线程锁深度解析:从概念到实战,避坑指南全掌握
linux·操作系统·线程池·进程·线程安全·c/c++·线程锁
♛识尔如昼♛6 天前
操作系统(4)第二章- 进程通信
操作系统·进程·ipc
_OP_CHEN7 天前
【Linux系统编程】(四十六)线程池原理与实现:从固定线程池到线程安全单例模式
linux·单例模式·操作系统·线程池·进程·线程安全·c/c++
青桔柠薯片20 天前
Linux软件编程:线程和进程间通信
linux·开发语言·线程·进程