Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I'm trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I'm not getting all of the matches that I see when I do the same thing in C# using the Regex object. Can someone explain what I am doing wrong?

Relevant PowerShell Script

$formattedMessage -match "(Get\sClient\sModel|Parse\sExpression|Get\sAbstract\sQuery|Compile\sQuery|Execute\sQuery|Get\sQuery\sPlan\sComplexity|Async\sTotal|Total)\s-\sduration\(([0-9]*)" | out-null
$matches

Output

Name  Value
----  -----
0     Get Client Model - duration(0
1     Get Client Model
2     0

Log Entry Example:

Timestamp: 11/9/2009 6:48:41 PM
Message:
Category: QueryService
Priority: 3
EventId: 1001
Severity: Information
Title: SPARQL Query Response
Machine: SPOON16-SERVER
App Domain: KnowledgeBaseHost.exe
ProcessId: 2040
Process Name: D:\QueryService\QSHost.exe
Thread Name:
Win32 ThreadId:8092
Extended Properties:
Key - Workflow_cbbdd58b-e574-4054-88d4-1dd7a56dc9d9
Timeout - 1800
Result Format - WireTable
Result from Registry - False
Compiled Query from Cache - True
Result Count - 28332
Query Plan Complexity - 661622
Get Client Model - duration(0) start(0)
Parse Expression - duration(0) start(0)
Get Abstract Query - duration(0) start(0)
Compile Query - duration(0) start(0)
Get Query Plan - duration(0) start(1)
Execute Query - duration(63695) start(1)
Get Query Plan Complexity - duration(0) start(63696)
Get Executed Operations - duration(0) start(63696)
Total - duration(63696) start(0)
Async Total - duration(63696) start(0)
like image 611
Eric Schoonover Avatar asked Nov 09 '09 19:11

Eric Schoonover


People also ask

How do I add a user to multiple groups in PowerShell?

Bulk add users to multiple groups from CSV file Run Windows PowerShell as administrator. Change the path to the scripts folder and run Add-ADUsers-Multi. ps1 PowerShell script. The script will go through all the users in the CSV file.

What is the function of $input variable in PowerShell?

$input. Contains an enumerator that enumerates all input that's passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions). In a function without a begin , process , or end block, the $input variable enumerates the collection of all input to the function.

What are the PowerShell command that enables the listing of users and groups?

Use Get-LocalUser PowerShell cmdlet to List All User Accounts. The Get-LocalUser PowerShell cmdlet lists all the local users on a device.


1 Answers

You can do this with the Select-String cmdlet in V2, but you need to specify the -AllMatches switch, e.g.:

$formattedMessage | Select-String 'regexpattern' -AllMatches

Keep in mind that with the -match operator the primary thing you are doing is looking for "a" match, i.e., is the regex pattern matched or not?

like image 189
Keith Hill Avatar answered Oct 11 '22 15:10

Keith Hill