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

结果如下

相关推荐
柏木乃一19 小时前
进程(2)进程概念与基本操作
linux·服务器·开发语言·性能优化·shell·进程
ikkkkkkkl2 天前
进程同步与死锁
操作系统·进程
_OP_CHEN2 天前
【Linux系统编程】(十四)深入 Linux 内核:进程优先级调度与切换的底层逻辑全解析
linux·运维·linux内核·进程·进程切换·进程优先级·调度算法
_OP_CHEN2 天前
【Linux系统编程】(十五)揭秘 Linux 环境变量:从底层原理到实战操作,一篇吃透命令行参数与全局变量!
linux·运维·操作系统·bash·进程·环境变量·命令行参数
CQ_YM3 天前
Linux进程基础
linux·服务器·进程
CQ_YM3 天前
Linux进程终止
linux·服务器·前端·进程
Trouvaille ~5 天前
【Linux】进程调度与环境变量:Linux内核的智慧
linux·运维·服务器·操作系统·进程·环境变量·调度算法
ベadvance courageouslyミ6 天前
系统编程之进程
linux·进程·pcb结构体
龙泉寺天下行走6 天前
[powershell 入门]第9天:PowerShell 安全、代码签名与企业部署 作业及深度解析
安全·powershell
課代表7 天前
bat 批处理文件中 PowerShell 命令换行问题
符号·参数·powershell·批处理·换行·续行符·管道符