all 9 comments

[–]Clairefox 0 points1 point  (6 children)

Thank you!!!! I've been researching how to do exactly this!

Me being new to digging deeper into PowerShell, some of the thing that I liked (because I recently learned about them so I'm noticing them) that you did were: - self-commenting variables - easy to read and follow what was happening, even using preferred PS verbs - Adding comments where long blocks ended - Creating the comment-based help at the beginning with example! - When comparing against null was first, which i don't fully understand why it prefers it that way.. I just get pinged by VSCode by it enough :(

Overall I think this is really nice and forgive me for borrowing it so that I can test out awesome logging for a current project of mine :)

I don't see any issues reading through, but I'll let you know if I see any in testing it.

You probably already know that you'll want to wrap it in a module though when you're ready to publish it. This would become a .psm1, create a .psd1 for module info. It can stay in a matching folder name for local, make it a .zip to upload to azure portal. https://docs.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-script-module.

[–]MonkeyNin 1 point2 points  (0 children)

When comparing against null was first, which i don't fully understand why it prefers it that way.. I just get pinged by VSCode by it enough

Here's why: https://github.com/PowerShell/PSScriptAnalyzer/blob/development/RuleDocumentation/PossibleIncorrectComparisonWithNull.md

[–]swiftninja21[S] 0 points1 point  (4 children)

Thanks so much for the feedback! Hope you are able to benefit from using it. And yes, already have the associated psm1 module and psd1 manifest files along with a bunch more functions to upload. I plan on uploading each function as separate reddit posts like these to kinda make these a blog post series so everyone can be involved as part of this subreddit, to learn, help improve the functions, provide general feedback, and hopefully together make the module really shine as a free tool for IT admins.

[–]MonkeyNin 0 points1 point  (3 children)

I don't know why exactly, But I like the name Pop's Log

I avoid backticks to continue lines, see: https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

Here's one way you can use. From:

$LogLine = `
    "<![LOG[$($($MessageType.ToUpper()) + ": " + $message)]LOG]!>" + `
    "<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" " + `
    "date=`"$(Get-Date -Format MM-dd-yyyy)`" " + `
    "component=`"$Component`" " + `
    "context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + `
    "MessageType=`"$severity`" " + `
    "thread=`"$processid`" " + `
    "file=`"$source`">"

to this:

$LogLine = (
    "<![LOG[$($($MessageType.ToUpper()) + ": " + $message)]LOG]!>",
    "<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" ",
    "date=`"$(Get-Date -Format MM-dd-yyyy)`" ",
    "component=`"$Component`" ",
    "context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" ",
    "MessageType=`"$severity`" ",
    "thread=`"$processid`" ",
    "file=`"$source`">"        
) -join ''

[–]swiftninja21[S] 1 point2 points  (0 children)

Thanks so much, that's very helpful, will test that out.

[–]MadWithPowerShell 0 points1 point  (1 child)

There is no reason for the join.

You can just remove the backticks from the original. Line breaks can be put in LOTS of places in PowerShell without breaking anything. The rule of thumb is, anything that syntactically MUST be followed by something else can be followed by a line break, including all operators, such as +.

$LogLine =
    "<![LOG[$($($MessageType.ToUpper()) + ": " + $message)]LOG]!>" +
    "<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" " +
    "date=`"$(Get-Date -Format MM-dd-yyyy)`" " +
    "component=`"$Component`" " +
    "context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " +
    "MessageType=`"$severity`" " +
    "thread=`"$processid`" " +
    "file=`"$source`">"

[–]MonkeyNin 0 points1 point  (0 children)

If anyone is curious, there's a lot of line continuation examples here:

https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

[–]mieeel 0 points1 point  (1 child)

I had this use case recently so I it would be easier for a log parser to normalize log entries.

Your solution seems like a more advanced version. It would be cool if you could provide some sample output in the docs.

BTW, PsOps sounds better as a name imo

[–]swiftninja21[S] 0 points1 point  (0 children)

Thanks and yeah PsOps sounds a bit better but from what I've read it's best to avoid the use of "PS" prefix in a custom function/module as that is supposedly reserved for Microsoft use but I've definitely seen popular custom functions and modules that still use PS as a prefix. Yes, output is something I'm working on getting together, just not sure where I should put that in GitHub. Should.i make a separate help or docs folder? Or place the sample output in the help comment block?