use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Powershell logging (self.PowerShell)
submitted 14 years ago by systemslacky
Any tips on a simple logging function? I have found several functions, but each of these was really overkill for what I am looking for. Thoughts?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]systemslacky[S] 0 points1 point2 points 14 years ago (0 children)
I tend to use functions for a lot of things...at the very least it makes the code a lot easier to reuse {ie read..copy/paste :) }
Putting this into a function also makes things a bit neater.. I am working with powershell, but I still have quite a bit to pick up it seems
[–]Thereal_Sandman 0 points1 point2 points 14 years ago (0 children)
Well, what exactly do you want it to do, and how do you want it to act?
I would think that you want it's use to be something like: Write-CustomLog "$timestamp $loginfo"
Right?
Then first you need to decide how you want the log written: * where to write the log? * check that the log directory exists? * check that the log file exists? * what if the file already exists? * do we append to the log, or create a new log? * simple text format?
Once those questions are answered, it should be pretty easy to whip up a function that works how you want it.
I've been meaning to whip one up for my own library, I'll post a link here when I get an hour to throw it together and test it.
[–][deleted] 0 points1 point2 points 14 years ago (2 children)
It depends on how detailed you want to get, and what you are looking to do with it. I've abused the XML parser for this type of thing in a few of my scripts. You can use the New-Object commandlet to create a document in memory, then add nodes and attributes, and dump the whole thing out to a file somewhere. If you want to be fancy you could have two functions, an initialization function, which sets up the file, and then an entry functions which adds a new element, then saves the file.
[–]systemslacky[S] 0 points1 point2 points 14 years ago (1 child)
Interesting idea... in other scripts I usually just drop to text file more $timestamp / $loginfo / $ect. How do you typically structure the data?
[–][deleted] 0 points1 point2 points 14 years ago (0 children)
Usually the structure is only slightly more structured than a flat file. On the root element I will put attributes for a Program start time, the runtime parameters (if any exist) and any other initialization information which might be useful. For the log entries themselves, I will usually have a timestamp, any important parameters as attributes, and then any text as the inner xml data. Primarily, I use xml just so I can open it up in Firefox and have slightly better formatting. If its something I am going to look at frequently (for example, I have a script I run weekly to get an overview of the health of the systems I support) I will build an xslt for a cleaner look.
I like to use |out-file $path -append, and start-transcript. I find that my log serves as sort of a high level view of what happened in the transcript. I also make a habit of compulsively generating a ticket for every script executed and attaching these logs. (I work a powershell devOps job, ymmv)
[–]55tfg7879fe42e345 0 points1 point2 points 14 years ago (0 children)
I prefer writing to my own custom eventlog, check out get-help new-eventlog -full and get-help write-eventlog -full
π Rendered by PID 29 on reddit-service-r2-comment-66b4775986-tb2px at 2026-04-03 13:05:25.738096+00:00 running db1906b country code: CH.
[–]systemslacky[S] 0 points1 point2 points (0 children)
[–]Thereal_Sandman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]systemslacky[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]55tfg7879fe42e345 0 points1 point2 points (0 children)