Solved

Powershell Regular Expression script question

Posted on 2011-02-18
3
553 Views
Last Modified: 2012-06-27
I am trying to write a script to read in a test file, search for just the user and return that.

SOMEDOMAIN\SomeUser             Full Control[aasdfa]
SOMEDOMAIN\SomeUser             Partial Control[aasdfa]
SOMEDOMAIN\SomeUser             Full Control[aasdfa]

I have some of the code needed and I also have the a regex statement that works.    I am getting caught on the output of $Matches
When I run the code and check $matches is only contains the last item found/returned.

I know I am missing something simple, any insight would be appreciated :)



Get-Content file.txt |where-object{$_ -match '\S*\s'}

Open in new window

0
Comment
Question by:fertigj
  • 2
3 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 34932277
This executes with an implicit loop:
Get-Content file.txt |where-object{$_ -match '\S*\s'}

Open in new window

Since Get-Content returns an array, all you'll have in $Matches is the last value. If you need to parse Matches it would be wies to execute this as an explicit ForEach-Object loop:
Get-Content file.txt | ForEach-Object {
  If ($_ -match '\S*\s') {
    # Do things with $Matches
  }
}

Open in new window

HTH

Chris
0
 
LVL 1

Author Comment

by:fertigj
ID: 34957650
ok.   I am sure I am missing something simple, but what if I just want to write this to the host  ie   write-host $matches.value.


When I do this I get nothing back.   Thoughts?
0
 
LVL 70

Assisted Solution

by:Chris Dent
Chris Dent earned 250 total points
ID: 34958885

$Matches is a HashTable, it doesn't have a property explicitly called "Value".

I think you want:

$Matches[0]

Which returns the first match, at Key 0.

Chris
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question