Author Posts

January 1, 2012 at 12:00 am

by BustedFlush at 2013-03-28 14:06:58

Hey, I have a log file with bunches of server and folder names, amongst stuff I don't need. Both of the items I need are in quotes. Ideally all I want are the server and folder names.

$a = get-content log1.txt
$regex = '\".*\"'
$lines = $a | Select-String -pattern $regex

This works fine, and I get all of the lines:
Target Server="SERVER7" Folder="DFSRoot$\ABC"
Target Server="SERVER8" Folder="DFSRoot$\DEF"
Target Server="SERVER9" Folder="DFSRoot$\GHI"

But attempting any further string manipulation fails, propblaly because they aren't string objects, but [Microsoft.PowerShell.Commands.MatchInfo] which 'doesn't contain a method named 'split"

So I tried something like foreach ($line in $lines){
$line.ToString()
$line.Split('=')}

But the .ToString() method doesn't seem to work; I still get 'Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'Split'.'

Not sure what I am doing wrong?

by mjolinor at 2013-03-28 15:46:23

Those matchinfo objects have a Line property that contains the text of the matched line, so:


$a = get-content log1.txt
$regex = '\".*\"'
$lines = $a | Select-String -pattern $regex | select -ExpandProperty Line

That being said, if you don't need anything but the matched text, you can do that much easier with just the -match operator:


$regex = '\".*\"'
$lines = (Get-Content log1.txt) -match $regex