【问题标题】:Extract an argument from a process' command line从进程的命令行中提取参数
【发布时间】:2018-12-15 19:42:58
【问题描述】:

我一直在捕获命令行值,我需要捕获命令行中的最后 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."
}

【问题讨论】:

  • @Remko:这不是重复的,因为链接的问题侧重于获取进程的命令行,这是这个问题的附带(已经包含解决方案特殊问题)。 this 问题的本质是:如何从包含空格分隔标记的字符串末尾提取一个 4 位数字?

标签: powershell tokenize powershell-4.0 text-extraction


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 2011-11-21
    • 2012-03-20
    • 2021-10-16
    • 2019-02-26
    • 1970-01-01
    • 2012-11-12
    • 2014-08-04
    相关资源
    最近更新 更多