从流程的命令行中提取参数

时间:2018-07-07 12:29:53

标签: powershell tokenize powershell-v4.0 text-extraction

我被困在捕获命令行值中,我需要捕获命令行中的最后4个数字

该屏幕快照来自Process Explorer

enter image description here

我的代码如下

$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."
}

1 个答案:

答案 0 :(得分:1)

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

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