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

结果如下

相关推荐
IOT那些事儿20 小时前
Windows PowerShell配置Qt5编译运行环境
windows·powershell·qt5
峥无2 天前
《read/write的秘密:文件描述符、重定向与用户态缓冲区》
linux·运维·服务器·进程
今夕资源网3 天前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码
a里啊里啊5 天前
软考-软件评测师:知识点整理(二)——操作系统基础知识
操作系统·线程·进程·软考·pv操作·软件评测师
Fanfanaas6 天前
Linux 系统编程 进程篇(五)
linux·服务器·c语言·网络·学习·进程
ShineWinsu7 天前
对于Linux:进程间通信IPC(命名管道)的解析
linux·c++·面试·笔试·进程·ipc·命名管道
ShineWinsu7 天前
对于Linux:动静态库的制作与原理的解析—下
linux·运维·服务器·进程·链接·虚拟地址空间·
fengyehongWorld9 天前
Powershell 注册.dll类库到系统
powershell
就叫年华吧丶9 天前
Git Bash、CMD 与 PowerShell 的区别详解
linux·git·命令行·powershell·cmd·gitbash
月巴月巴白勺合鸟月半10 天前
从 Delphi 到 FMX:Borland/Embarcadero 跨平台开发三十年兴衰史
跨平台·delphi