Drinking the PowerShell Kool-Aid...Repost

This is a reposted blog from my now dead wordpress site so formatting may be poor...

So I’ve recently drank the kool-aid and I’m all in on PowerShell. After TechEd this year and speaking with lots of IT Pros & Industry folks, I knew it was time for me to get up to speed on PS or be left behind.

To this end, I have been working through Learn Windows PowerShell 3 in a Month of Lunches, 2nd Edition by Don Jones and Jeffery Hicks. This IS the place you should start if you are a PS newb like my self, or is the proper term PoSHnewb. Don and Jeff do a remarkable job of teaching PowerShell the way IT Pros work. For the most part, we are not programmers; we’re technicians, administrators , or architects, and that is how we need to use PowerShell; as a tool in which to perform singular or repetitive tasks, mostly from the CLI and sometimes as a script.

I like this book so much that I am having my 4th semester students go through the book to learn PowerShell in a way that will prepare them for the real world. They’ll need the help as I threw a curveball into the usual mix of work for my Windows Server Pro class. Normally, each student completes an individual case study where they build out an SMB windows network in VMs. This semester, I scaled back some of the case study tasks to reduce complexity of the overall network, and now I am requiring them to build the entire Windows Server 2012 network with PowerShell. How crazy is that???? I am doing the project along with them so I can hopefully catch and “gotchas” before they do.

So as I’m working my way through my PowerShell journey, I thought it would be cool to share code examples as they get created. Let me warn you…I can barely script my way out of a paper bag so scripts will be functional but probably could use some streamlining.

In any case, here’s the first useful script I wrote called DumpHistory.ps1. This script is a simple PS script to display and create a text file of all commands executed in the CLI during a specific session. The reason I came up with this was I teach my students to use get-history as a way to see what they have done, as well as keep track of their work. I wanted a way to easily create dumps of my history so I can review them at a later date as well as prep for demos. Again, I am sure someone else has done this, and probably cooler. That’s not the point. For me, it’s about thinking about process I do on a regular basis and seeing if I can create a tool to automate that process to some level.
####dumphistory.ps1
##Create a text file containing history of PowerShell Commands based on input file name and current date
##Created by Michael Bender
##Created/Revised 10/9/2013
##
##Create variable for current date
$Date=get-date
$Dy=$Date.Day
$Mo=$date.Month
$yr=$date.Year
##Get User Input for Name of File
$History=Read-Host “Enter Name for History file”
##Retrieve current CLI history and output to file
Get-History
Get-History | Out-File C:\Users\mikeb\SkyDrive\Matc\Fall2013ServerPro\PowerShell\historyfiles\$History.$mo-$dy-$yr.txt

Note: I have the out-file location hard-coded since I use the same location all the time. If you needed to specify the location at each running, the script could be modified by creating a variable for the location with read-host.

A zipped copy of the file is located http://sdrv.ms/1gqqHGe

If you have any suggestions on cleaning this up, please add in the comments.

Till next time…Mike


Comments

Justin said…
Hey Mike! Great to see PS is the scripting language of choice these days. Really wish I would have had the chance to take the PS class at MC. Luckily, my current job has been a great advocate of my learning PS. I've been completely addicted to automating frequently completed tasks and the vast ability to pull info that can't be gained from a GUI is amazing. Its definitely my favorite administration tool.

I dig the script. I prefer to process the date a bit differently and put the file output in list form to prevent truncation in the log. Here's how I do it:

###
$History = read-host “Enter Name for History file”
get-history
get-history | format-list | out-file ( 'C:\foo\' + $History + '.' + (get-date -format MM-dd-yyyy) + '.txt' )
###

-Justin Lewis