7

I am trying to write a simple PS script to check large .txt log files for a short string: "SRVE0242I:"

$lines = Select-String -Path $logDir -Pattern "SRVE0242I:" | Select-Object line | Out-String

On output though, it only displays the following:

Line
[28/06/17 13:48:27:839] 00000020 ServletWrappe I SRVE0242I: [User] [User] [com_xxxxxxx_...

And not the full line. Is there a limit to how many characters this pulls? I can't find any info on any restrictions for the Select-String cmdlet. Is there a better way to do this so that I don't a) pull the heading "Line" in my list of lines (Don't really want to create table formatting for such a simple output) and b) get the whole line when I pull the info?

user3431504
  • 149
  • 1
  • 1
  • 9
  • Instead of `Select-String -Path $logDir -Pattern "SRVE0242I:" | Select-Object line` you can probably use `(Select-String -Path $logDir -Pattern "SRVE0242I:").line` This gets rid of output length restriction of select-string. – Sahil Singh Jun 04 '18 at 13:37

2 Answers2

9

You are seeing it like this because it's displaying the Line property using the default Format-Table view and shortening it to the width of the console.

Do this instead:

$lines = Select-String -Path $logDir -Pattern "SRVE0242I:" | Select-Object -ExpandProperty line

This returns the value of the Line property as a string to the $lines variable. You don't need to use Out-String.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
2

There is! Long story short, Select-Object is doing the truncating here. Here's one way to get the first untruncated line in a Select-String output

$(Select-String -Path $logDir -Pattern "SRVE0242I:")[0].Line

When you run into something like this, you can break down the individual steps to determine what's happening by piping things to Get-Member. Here's what's happening in the code above:

Select-String <# args #> | Get-Member

Select-String gives us a MatchInfo object, which (as you've correctly determined) has a 'Line' property. When run on it's own, Select-String will actually spit out all the information you're looking for, and will not truncate it by default (at least, on v6.0.0-beta). It does give you an array of MatchInfo objects if it finds multiple matches, so you have to index into that array if you just want the first one (like I did above).

Select-String <# args #> | Select-Object Line | Get-Member

Select-Object applies PowerShell's default formatting for objects which, in most cases, will truncate your output for easier viewing. For objects with a bunch of members (like a MatchInfo object), it will try to do one per line by default.

Select-String <# args #> | Select-Object Line | Out-String | Get-Member

Out-String directly translates it's input to a string. That is, rather than trying to cast something to a string or pull a string Property out of an object that's passed to it, it just changes whatever it receives into an object. In this case, it turns the already-formatted MatchInfo output into a string. Nothing happens to the output on the terminal, but Get-Member will reveal a String rather than a MatchInfo object.

It's not directly relevant here, but if you're interested in modifying the default formatting, it's governed by the types.ps1xml file.