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

该篇文章介绍了如何在Delphi程序中使用PowerShell脚本来管理和操作Windows进程,包括列出进程、按ID或名称筛选以及终止进程。展示了通过TStringList和StringGrid组件处理PowerShell命令输出的过程。

 列出当前运行的进程:

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列出详细信息

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列出详细信息

var
  PowerShellPath, ScriptPath, CommandLine: string;
begin

  PowerShellPath := 'powershell.exe '; // 假设 PowerShell 可执行文件在
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值