首页 > 解决方案 > 从进程的命令行中提取参数

问题描述

我坚持捕获命令行值,我需要捕获命令行中的最后 4 个数字

屏幕截图来自 Process Explorer

在此处输入图像描述

我的代码如下

$process = "notepad.exe"
$CommandLine_QID = Get-WmiObject Win32_Process -Filter "name = '$process'" |
                     Select-Object CommandLine # just capture the command line 

我需要从命令行拆分最后 4 位数字并从这里存储在一个变量中。

$Process_PID = Get-Process -Name "notepad" -ErrorAction SilentlyContinue  | Select-Object ID  

然后,我需要使用 $CommandLine_QID 的变量值对存储在数据库机器中的变量进行交叉检查

eg: db_var1 = 9998
if($CommandLine_QID -contain db_var1)
{
write-host "value contained."
}

标签: powershelltokenizepowershell-4.0text-extraction

解决方案


最简单的方法是使用 RegEx\d+$从命令行中提取尾随数字:

$process = "notepad.exe"
$CommandLine_QID = [RegEx]::Match( 
    (Get-WmiObject Win32_Process -Filter "name = '$process'").CommandLine,'\d+$'
).Value

推荐阅读