loading...
Cover image for How To Not Write A Blog Post

How To Not Write A Blog Post

theinfradev profile image TheInfraDev Originally published at theinfradev.pro ・4 min read

Thoughts on writing a blog

My phone buzzes, it is an alarm that goes off every 2 weeks on a Friday.

"You have 48 hrs to write a blog post."

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.

I open my standard blog template, with markdown ready to go.

...The cursor blinks forlornly. A call to action if I ever did see one.

"You brought this on yourself", I mutter under my breath.

I have a headline idea but there are all these stupid questions in my way...

I thought this was a blog about writing shitty scripts? I think you have a bad case of "Gone Meta"

It goes something like this.

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.

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...?

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.

Let’s take a step back

For that, do you not have to take a step forward first?

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?

Bastard.

Let me leave you with something useful!

It’s really the least you can do after this rambling mess

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.

First declare an environmental variable at the start of your script with the log location like so:

$env:Logfile = "$AppPath\logs\$(get-date -format 'dd-MM-yyyy') - Application log"

#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.

Then we will write the function itself.

Function Write-Log

{

    Param([string]$LogEntry,[string]$Logpath,[string]$Color)
      # We define our inputs #

    $DateTime = Get-Date -format 'dd/MM/yyyy HH:mm:ss' ; if ($Color -notmatch "\w"){$Color = 'Gray'}

      # Then we get the current date/time, and make sure we have a color choice (Gray as a default) #

    $log = "$env:Computername : $DateTime : $LogEntry"

      # Now we create our full log entry #

    Out-File -InputObject $log -FilePath $LogPath -Append -NoClobber

      # This is how we write it to a file #

    Write-host $Log -ForegroundColor $Color

      Finally we write it to the console so we can debug as we write the app.

}



So what have we created? Well if we run the function like so:

Write-Log -LogEntry "I wrote a nice log file" -Logpath $env:Logfile -color 'blue'

We will likely now have a new log at the desired location and a nice message on the console in blue like this:

Computer01 : 19/12/2018 06:48:02 : I wrote a nice log file

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...

The InfraDev

To get updates on my new articles, follow me on Twitter @theinfradev

Posted on by:

theinfradev profile

TheInfraDev

@theinfradev

I am an Infrastructure Developer, writing about the challenges of writing infrastructure as code.

Discussion

markdown guide