使用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;

结果如下

相关推荐
DONSEE广东东信智能读卡器4 小时前
用PowerShell实现Windows 本地 WSS/HTTPS 自签名证书配置方法
windows·网络协议·https·powershell·身份证阅读器
DogDaoDao7 小时前
Windows 下 Git 报错:`touch` 无法识别 —— 原因分析与 7 种解决方案(从入门到精通)
windows·git·程序员·npm·powershell·cmd·touch
长谷深风1112 天前
Java并发编程:线程安全与多线程实战指南【个人八股】
java·安全·线程·进程·juc·并发与并行·上下文切换(性能影响因素)
console.log('npc')3 天前
Windows 11 安装 WSL2 + Ubuntu + Docker + Codex + Sub2API 教学
windows·docker·powershell·ubantu·codex
Irissgwe3 天前
九、Linux信号机制(二)
linux·进程·可重入函数·volatile·sigchld信号·进程信号
CV工程师(Ctrl)3 天前
Codex 在 Windows 中文路径执行bash命令编码问题复盘
powershell·codex
vortex56 天前
PowerShell 的命令补全方案: PSReadLine + PSCompletions + argc + Carapace
windows·powershell
H Journey7 天前
Linux查看进程命令
linux·运维·进程
昆曲之源_娄江河畔9 天前
DBGridEh Footer的使用
前端·数据库·delphi·dbgrideh
Irissgwe9 天前
四、进程控制(进程等待与进程程序替换,shell)
linux·shell·进程·进程等待·进程程序替换