Avatar of Zack
ZackFlag for Australia

asked on 

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.


Powershell

Avatar of undefined
Last Comment
Dale Harris
Avatar of chrismerritt
chrismerritt

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.
Avatar of Zack
Zack
Flag of Australia image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of chrismerritt
chrismerritt

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Zack
Zack
Flag of Australia image

ASKER

Thank you for your help
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

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
Avatar of chrismerritt
chrismerritt

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
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

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!
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo