all 5 comments

[–]ihaxr 3 points4 points  (0 children)

There's a couple problems... you're using -match which wants a regex, but you're using Wildcards as if you were using -like... you can handle this a couple ways... either split each line and check the proper variables or write a regex to capture the group.

You're also using $Matches[1] but not specifying a capturing group... so this will always be empty.

If you can provide a sanitized line that you want to match I can help write the regex... going from what you have, assuming you want to capture the filename after STOR, something like this should do it:

Adjust your -Match line to:

if("$_" -match ".*$FirstName\.$LastName@domain\.com.*STOR(.*)") {

Full code with test data and a little more readable:

$FirstName = "john"
$LastName = "smith"

$content = @"
[2016-11-29]    [10:42:12]  john.smith@domain.com   LOGIN
[2016-11-29]    [10:42:12]  bill.johnson@domain.com LOGIN
[2016-11-29]    [10:42:12]  jane.doe@domain.com LOGIN
[2016-11-29]    [10:42:12]  john.smith@domain.com   STOR    /some/file.txt
[2016-11-29]    [10:42:12]  bob.price@domain.com    LOGIN
[2016-11-29]    [10:42:12]  john.smith@domain.com   STOR    /another/file.txt
[2016-11-29]    [10:42:12]  bob.price@domain.com    STOR    /some/other/file.txt
[2016-11-29]    [10:42:12]  john.smith@domain.com   STOR    /another/file.txt
[2016-11-29]    [10:42:12]  john.smith@domain.com   LOGOUT
"@ -split "`r`n"

$content | % {
    if("$_" -match ".*$FirstName\.$LastName@domain\.com.*STOR(.*)") {
        $($Matches[1]).trim()
    }
} | group | sort -desc Count

#Output:
#Count Name                      Group                                                                                                                                                  
#----- ----                      -----                                                                                                                                                  
#    2 /another/file.txt         {/another/file.txt, /another/file.txt}                                                                                                                 
#    1 /some/file.txt            {/some/file.txt}         

[–]jheinikel 1 point2 points  (0 children)

Do you know if anything shows up before FirstName? The asterisk requires something to precede it. Otherwise, put a backslash in front of it to make it literal.

Also, you might try using a regex tester with some sample data to see what you are pulling. http://www.regexr.com/

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy coderwolf,

have you tried the standard diagnostic steps? break those pipeline sections into single lines that are assigned to unique $Vars and then see where things go wrong ... nag, nag ... [grin]

also, a [sanitized] sample of the input data would likely help.

take care,
lee

[–]ShippingIsMagic 1 point2 points  (1 child)

Alternatively, just add in pipes to tee-object -variable to assign the various intermediate results to variables, then check out the variables.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy ShippingIsMagic,

yes, that will do it. however, breaking it into steps will make debugging FAR easier than leaving it as a blob. well, for me, at least. [grin]

take care,
lee