bob0the0mighty
bob0the0mighty

Reputation: 782

Return Select-String results with context from Invoke-Command (appear blank)

I'm trying to search log files on multiple servers using Invoke-Command and return the results with context but I'm having no success.

This is the command:

Invoke-Command -Session $session {
  cd C:\LogDir\Log1
  sls -Pattern "ThingImLookingFor" -Path * -Context 1,5 
}

This returns a deserialized MatchInfo object with most info dropped.

How would I get results that look similar to running Select-String locally?

This is the result of running sls on an svg in my home dir with the same context settings as an example:

  horizontalBlock.svg:30:     id="base"
> horizontalBlock.svg:31:     pagecolor="#ffffff"
  horizontalBlock.svg:32:     bordercolor="#666666"
  horizontalBlock.svg:33:     borderopacity="1.0"
  horizontalBlock.svg:34:     inkscape:pageopacity="0.0"
  horizontalBlock.svg:35:     inkscape:pageshadow="2"
  horizontalBlock.svg:36:     inkscape:zoom="1.3289991" 

Upvotes: 0

Views: 1763

Answers (2)

js2010
js2010

Reputation: 27516

For some reason, the custom format from the returned psobject appears blank. Here's another workaround.

Invoke-Command -Session $session {
  cd C:\LogDir\Log1
  select-string -Pattern "ThingImLookingFor" -Path * -Context 1,5 | select line
}

Or

Invoke-Command -Session $session {
  cd C:\LogDir\Log1
  select-string -Pattern "ThingImLookingFor" -Path * -Context 1,5 
} | select line, pscomputername

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36322

Pipe sls to Out-String:

Invoke-Command -Session $session {
  cd C:\LogDir\Log1
  sls -Pattern "ThingImLookingFor" -Path * -Context 1,5 | Out-String
}

Upvotes: 1

Related Questions