[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

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.


0
Zack
Asked:
Zack
  • 3
  • 2
  • 2
1 Solution
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
ZackGeneral IT Goto GuyAuthor Commented:
Thank you for your help
0
 
Dale HarrisCommented:
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 HarrisCommented:
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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now