Pergunta

I'm trying to parse a text file with this format:

\\fileshare40\abccheck\logons\ABC64ZXZ.txt:5398:UserID:  abcusernamehere Logged:  09:18:36 2014/03/13
\\fileshare40\abccheck\logons\ABC63BZB.txt:5403:UserID:  abcusernamehere Logged:  01:21:31 2014/03/14
\\fileshare40\abccheck\logons\ABC61ZSF.txt:5408:UserID:  abcusernamehere Logged:  08:22:31 2014/03/17
\\fileshare40\abccheck\logons\ABC62ETB.txt:5413:UserID:  abcusernamehere Logged:  07:58:52 2014/03/18
\\fileshare40\abccheck\logons\ABC60BBB.txt:5418:UserID:  abcusernamehere Logged:  13:11:36 2014/03/19

The only thing I need out of here is the machine name (ABC*****). Later on I'll put it into an array to see what if there are duplicates, but the answer here will get me started on that path.

I've tried this:

$abc = select-string -path c:\users\abcusernamehere\desktop\findusermachines.txt -pattern "TCWS....." -allmatches

But doing so displays the whole line of text in that file. How can I break up the line to JUST find and display what I'm searching for?

Foi útil?

Solução

For that, you can use a regex:

(get-content c:\users\abcusernamehere\desktop\findusermachines.txt) -replace '.+\\(.+?)\.txt:.+','$1'

ABC64ZXZ
ABC63BZB
ABC61ZSF
ABC62ETB
ABC60BBB

There's really no point in using Select-String to search for the lines that have the server names if they all have one. Just use Get-Content, and run the -replace operator against all the lines.

Outras dicas

You can do that with a regex match.

[regex]::Matches((gc c:\users\abcusernamehere\desktop\findusermachines.txt),"ABC.....")|select -ExpandProperty value

That spits back:

ABC64ZXZ
ABC63BZB
ABC61ZSF
ABC62ETB
ABC60BBB

Yet an other answer :-)

${c:\users\abcusernamehere\desktop\findusermachines.txt } | ? { $_ -cmatch "\b(?<MACHINE>ABC.+)\b.txt" }  | % { $Matches['MACHINE'] }

ABC64ZXZ
ABC63BZB
ABC61ZSF
ABC62ETB
ABC60BBB

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top