How To Not Write A Blog Post
Tags: powershell, posh, devops, sysadmin
Published: 2018-12-23 10:48:27


---
title: How To Not Write A Blog Post
published: true
description: In this pre-Christmas post I get a bit meta as I focus on the troubles of a writer so I don't have to write anything.
tags: [PowerShell,PoSH,devops,sysadmin]
canonical_url:  https://theinfradev.pro/06-howtonotwriteablogpost.html
cover_image: https://thepracticaldev.s3.amazonaws.com/i/5lgnsmzyrx4vdnft6a40.jpg
---

<h1><b>Thoughts on writing a blog</b></h1><p>
My phone buzzes, it is an alarm that goes off every 2 weeks on a Friday.<p>
<blockquote>
"You have 48 hrs to write a blog post."
</blockquote><p>
It is meant to be a motivating reminder, yet it is received more like a kidnapper’s ransom demand. A forceful reminder that it is time to get two weeks of random thoughts compressed onto the page, ready for review by my beautiful wife (to make sure I didn’t make any horrendous errors, like using there instead of their, or trying to figure out if it is accept or except). Ready for publishing to the world, ready for the glare of the dazzling lights of my own hubris, by next Wednesday* at the latest.
<p><p>I open my standard blog template, with markdown ready to go.<p>
...The cursor blinks forlornly. A call to action if I ever did see one.
<p><p>
"You brought this on yourself", I mutter under my breath.
<p><p></p>
<h1><b>I have a headline idea but there are all these stupid questions in my way...</b></h1>
<h3><i>I thought this was a blog about writing shitty scripts? I think you have a bad case of "Gone Meta"</i></h3>
<p> It goes something like this.<p><p> For my next blog I have a grand idea, how to use classes in PowerShell to aid the building of a great script. But I don’t like extremely long blog posts. I want to keep it at about 600-1000 words. And with that in mind, how do you condense everything that is great about encapsulation and composition and polymorphism to that size - to an audience that may or may not know what those things are? How do you NOT start at the beginning? I don’t want to start at the beginning, better developers than I have done that, and I should only refer to their esteemed work rather than stumble over it badly.<p><p>
But in the meantime I do have something important to say about classes, about POPO’s (Plain Old PowerShell Objects. Get it? Well if you aren't a dev - probably not. haha... ahem.). There’s the stuff about using classes to hide your implementation of methods and lastly how you can then use those building blocks to make really cool functions that work 95% of the time, every time.
Shit, how do you explain interfaces...?<p><p>

I am only like 5 blog posts in, the first couple were more "about the mission" a.k.a. why I want to do this. Whereas this stuff, this is the good stuff. This is what people actually want to learn and this is what I have to give. And yet, can the 6th post instead be a meta description of blogging? Does everyone feel like this? Do all the people who - with good intention - set out to write a little about what they know and love, stumble upon this road where the possibilities are infinite, the likelihood of sounding like a gibbering mess is high and... really, please forgive me, I meant well... Oh dear.
<p><p></p>
<h1><b> Let’s take a step back</b></h1>
<h3><i>For that, do you not have to take a step forward first?</i></h3>
<p>
The cursor blinks forlornly. I look up, I have an idea. I’ll make it a three-part series. Yeah, that’s it. The first part is about creating simple classes to pass around data, then the methods, finally I’ll wrap it all in a nice function. Brilliant, pat on the back. Now, what will I use as my example?
...
ah nuts...
The cursor blinks forlornly. 
How does it do that? How can a cursor be forlorn? <p><p>Bastard.
<p><p></p>
<h1><b> Let me leave you with something useful!</b></h1>
<h3><i>It’s really the least you can do after this rambling mess</i></h3>
<p>
As the Holiday season is basically now and (you might have guessed) I have postponed the first of the all! Important! Three! Part! Series! till January, let me leave you with a gift. Something that I have can write by heart now. How to write a simple but effective logging function in PowerShell. This will always be a good idea to implement near the start of your fun little scripts, I promise.
<p><p>
First declare an environmental variable at the start of your script with the log location like so:
<blockquote>
$env:Logfile = "$AppPath\logs\$(get-date -format 'dd-MM-yyyy') - Application log"<p> #The get-date temporary variable here will make sure to add the date to the file name so you don’t end up with one giant 100MB log.
</blockquote>
<p>
Then we will write the function itself. <p>
<blockquote>
Function Write-Log <p>
{<p>
<ul>
    Param([string]$LogEntry,[string]$Logpath,[string]$Color) <p><ul># We define our inputs #</ul><p>
    $DateTime = Get-Date -format 'dd/MM/yyyy HH:mm:ss' ; if ($Color -notmatch "\w"){$Color = 'Gray'} <p><ul># Then we get the current date/time, and make sure we have a color choice (Gray as a default) #</ul><p>
    $log = "$env:Computername : $DateTime : $LogEntry"  <p><ul># Now we create our full log entry # </ul><p>
    Out-File -InputObject $log -FilePath $LogPath -Append -NoClobber  <p><ul># This is how we write it to a file #</ul><p>
    Write-host $Log -ForegroundColor $Color  <p><ul>Finally we write it to the console so we can debug as we write the app.</ul>
</ul>
}<p>
</blockquote>
So what have we created? Well if we run the function like so:<p>
<blockquote>
Write-Log -LogEntry "I wrote a nice log file" -Logpath $env:Logfile -color 'blue'
</blockquote>
We will likely now have a new log at the desired location and a nice message on the console in blue like this:<p>
<blockquote>
Computer01 : 19/12/2018 06:48:02 : I wrote a nice log file 
</blockquote>
I hope this helps, and if you use it - that you enjoy having nicely formatted logs! Have a Merry Christmas and/or Happy Holidays.
See you next year for my amazing three-part series on PowerShell Classes and Functions and why they are so awesome together...
<p>
The InfraDev
</p>
<i>To get updates on my new articles, follow me on Twitter <a href="https://twitter.com/theinfradev" target="_blank" rel="noopener">@theinfradev</a> </i>