Powershell Logging

Hi Guys/Gals,

I have question how would I go about outputing to log file errors in a csv format. For example errorid, message, application etc.

I know to write to log file in general you would use thing something like:

Start-Transcript -path $LogFile -force
write-host "Error has occured"

How would one utilize the output of the write-host command so it output a message like:
0012 - Tab - Error has occured - Tab - Explorer.exe
The column name being on top of the csv file.

Any help would be much appreciated.

Cheers.


ZackGeneral IT Goto GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chrismerrittCommented:
Hmmm, there are a myriad different ways of logging errors in PowerShell.

Sounds like you want to export into a CSV format though, tell me, can you write out the log file at the end of processing your script, or do you need to write out the log file on the fly?

If you can wait to the end then you could pipe your errors into an Array object and then export that to CSV afterwards.

If you need to write out a tab delimited file on the fly you could try this, I am assuming your error messages are in made up variables:

"$Error" + "`t" + "$Message" + "`t" + "$Process" | Out-File "C:\Some Folder\Some File.txt" -Append

This would write out the errors to the file and it should also tab delimit them. The `t character is a tab according to PowerShell.
0
ZackGeneral IT Goto GuyAuthor Commented:
Hi Chrismerritt,

I would need to write out the log file on the fly. Tried what you said below doesn't seem to be working see screenshot.

"$Error" + "`t" + "$Message" + "`t" + "$Process" | Out-File "C:\Some Folder\Some File.txt" -Append

Thank you.

Screenshot.JPG
0
chrismerrittCommented:
Please try this:

$ErrorMessage = "error"
$Message = "Message with spaces"
$process = "explorer.exe"

"$ErrorMessage" + "`t" + "$Message" + "`t" + "$Process" | Out-File "C:\TEMP\file.txt" -Append

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

ZackGeneral IT Goto GuyAuthor Commented:
Thank you for your help
0
Dale HarrisProfessional Services EngineerCommented:
I just want to point out an easier way to write the 5th line:

"$ErrorMessage" + "`t" + "$Message" + "`t" + "$Process"
can be written as:
"$ErrorMessage`t$Message`t$Process"

The thing to remember is use double quotes to allow for powershell to figure out what you're trying to say.  Also the backtick (`) is a special command to Powershell to escape one character forward.  If you put a $ in there or whatever other character that's not special, it will read it as a literal dollar sign, not a variable.  If you put it at the end of a line, it will be continued on the next line.  If you put a n or t after it, it will make a new line or a tab.  Those are some of the ways you can use an escape character.  You no longer have to separate it like VBScript.

"Hello " & Name & ".  How are you?"

HTH,

DH
0
chrismerrittCommented:
Whilst it can be written as that yes, I deliberately seperated the content keeping the `t's seperate, otherwise it all kind of merges into one :P
0
Dale HarrisProfessional Services EngineerCommented:
Oh okay, sorry to intrude.  You're right, it does keep it easy to read.  Sometimes I overlook readability for less typing, which is not always great :)  Keep up the good work!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.