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

结果如下

相关推荐
JUNIOR_MU1 天前
【VMware VCF】使用 PowerVCF 连接和管理 VMware Cloud Foundation 环境。
powershell·vcf·vmware cloud foundation·powercli·powervcf·power validated solutions·request-vcftoken
豆小匠Coding4 天前
Windows安装Vim,并在PowerShell中直接使用vim
linux·编辑器·vim·powershell
w_outlier8 天前
进程间通信(一)【管道通信(下)】
linux·进程·通信·管道
山顶风景独好11 天前
【apt-file命令详解:Linux下的包管理利器】
linux·运维·数据库·操作系统·进程·命令
pcplayer13 天前
WEB 编程:使用富文本编辑器 Quill 配合 WebBroker 后端
前端·后端·delphi·web开发·webbroker
GGBondlctrl13 天前
【后端开发】JavaEE初阶——计算机是如何工作的???
java·java-ee·操作系统·进程·冯诺依曼体系·计算机工作原理
加油,旭杏14 天前
【Linux】Linux进程的概念
linux·运维·服务器·进程·冯诺依曼体系结构·管理·pcb
aroc_lo14 天前
DELPHI编译软件时带上当前IDE的版本号
ide·delphi
sensor_WU15 天前
【Delphi】Delphi 中的 LiveBindings 使用场景与概念
delphi·livebindings
c4fx16 天前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll