0

I am stuck with capturing the command line values , I need to capture the last 4 number in the command line

The screen shot is from Process Explorer

enter image description here

my code is as follows

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

I need to split the last 4 digit number from command line and store in a variable from here.

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

I then need to cross check with a variable stored in DB machines with the the variable values of $CommandLine_QID

eg: db_var1 = 9998
if($CommandLine_QID -contain db_var1)
{
write-host "value contained."
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
zar
  • 1
  • 1
    Possible duplicate of [How to get Command Line info for a process in PowerShell or C#](https://stackoverflow.com/questions/17563411/how-to-get-command-line-info-for-a-process-in-powershell-or-c-sharp) – Remko Jul 07 '18 at 13:01
  • @Remko: It's not a duplicate, because the linked question focuses on obtaining a process' command line, which is _incidental_ to this question (which already contains the solution to that particular problem). The essence of _this_ question is: how can I extract a 4-digit number from the end of a string containing whitespace-separated tokens? – mklement0 Jul 07 '18 at 16:48

1 Answers1

1

The easiest is to use a RegEx \d+$ to extract the trailing digits from the command line:

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