[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell Regular Expression script question

Posted on 2011-02-18
3
Medium Priority
?
580 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 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 71

Assisted Solution

by:Chris Dent
Chris Dent earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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