1

I want to know the status of my running process from the detailed description using REGEX in Windows powershell scripting. I want to extract RUNNING from this string

Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00

4
  • 1
    Welcome to stackoverflow, could you detail what you've tried and what your expected results are, you'll get better quality answers if you do. Commented Feb 7, 2013 at 13:13
  • 2
    Wouldn't it be easier to check whether "Status RUNNING" is part of the string? There appears to be no need to use a regex (unless you want to guard against the case that a process might contain these words in its name, which you would need to specify in your question). Commented Feb 7, 2013 at 13:14
  • Split the string according to space and loop through the result. Here's an example on how to do it Commented Feb 7, 2013 at 13:34
  • @ClydeFrog a simple regex can extract it easier and more fool proof. Commented Feb 7, 2013 at 14:00

3 Answers 3

3

Using -replace

$text = 'Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00'
$text -replace '.+\sStatus\s(\S+)\sCheckpoint.+','$1'
RUNNING
Sign up to request clarification or add additional context in comments.

Comments

1

To extract RUNNING or STOPPED etc. you can try the following:

PS > $s = "Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00", "Name: Process2_name Started 2008-04-21 11:33 Status STOPPED Checkpoint Lag 00:00:00"

PS > $s | % { if ($_ -match "Status (.+) Checkpoint") { 
    #Return match from group 1
    $Matches[1] 
    }
}

RUNNING
STOPPED

If you're reading a log-file, you can send the content of it directly to the test like this:

PS > Get-Content mylog.txt | % { if ($_ -match "Status (.+) Checkpoint") { 
    #Return match from group 1
    $Matches[1] 
    }
}

This is extracting everything between "Status " and " Checkpoint", as long as it's at least 1 character (can be mulitple words also).

3 Comments

I think This will do the job. Thanks Graimer
Hello Graimer, I got what i was lookin for. But there is something more which i would like to work upon. If in my $s variable is multiline with many instances of status like: "Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00 Name:Process_name2 Started 2008-04-21 11.34 Status Running Checkpoint Lag 00:00:00" ,how can i get the output as status for both process at same time. like it shud be RUNNING and then again RUNNING. I am very much new to powershell scripting. Please help me out with this. Thank You
Hey. See updated answer. It now takes an array(multiline) and sends one by one through the pipeline (|) into a foreach loop (%) that looks for status and extracts it in one line at a time.
0

If you're looking for Windows processes, doesn't it make more sense to use Get-Process which supports properties and filtering, or the WMI equivalent, gwmi Win32_Process?

Might be way off the mark here so correct me if I'm mistaken!

1 Comment

The process i am trying to catch is not a windows process. i mean to say that it doesn't create any service or process in task managaer. It just runs in background. So i am not able to catch the status of process using Get-Process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.