2

I'm using the select-string in my powershell script

Select-String D:\logs\xyz.log -pattern "error"

works fine but it gives me back ALL lines of my logfile which include the keyword "error". As I'm sending an email later with the detected logs a readable structure would me nice, but the content of the select-string does not include any structure.

Best solution would be to add <br> after every detected line, because the mail is in html format and this would solve my problem. I was looking for any parameters of the select-string method but I didn't find a proper solution.

Any ideas?

2 Answers 2

4

I think this might be at least close to what you want to do:

$ErrorLines = 
(Select-String D:\logs\xyz.log -pattern "error" |
Select -Expandproperty Line) -join '<br>'
Sign up to request clarification or add additional context in comments.

2 Comments

This works fine, but: Before using this join, my result was like: path+line including the keyword. ex: "D:\logs\xy.log:Error:User not found." Now it's only the line content, without the path before. Any idea why? However, this is not a problem. I can print/insert the path manually as well.
Got it. Of course I should remove the 'Expandproperty' if I want to get path, linenumber and line.
2

How about formatting the output into an HTML table like so:

Select-String D:\logs\xyz.log -pattern 'error' | Select Line -ExpandProperty line | ConvertTo-Html -Fragment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.