The Power of Powershell

My brother-in-law’s 48″ muskie caught while pre-fishing for the 2015 Rhinelander Hodag muskie challenge was an epic battle. The fish hit the lure 10 – 15 feet from the boat, yet managed to stay in the water for what felt like several minutes before submitting to the net. Having the memory of a different large fish that we had lost right at the net about a month earlier, I was desperate not to mess this one up. I wanted to get the net ready as soon as possible, so instead of hooking my lure to the clip right above the reel, I reeled it up as close to the end of the pole as the leader would allow. This ended up being a mistake since the fish swam circles around the boat several times, and I had to carefully move the pole each time. At the same time, I was holding the net and trying not to get it hooked on anything inside the boat. It was a high pressure situation where I had to do several things at once, and we ultimately succeeded in landing the giant fish.

Jason's muskie
Powershell is great for doing multiple things at once. Maybe not technically at the same time, but the skilled DBA can create scripts to automate many different tasks which can be run one right after another. This is a great way to accomplish a large amount, which would take quite a while to do manually. I have a set of scripts that create an Excel workbook each day with a set of metrics about each of my instances, allowing me to see any possible problems immediately upon getting in each morning. The script shows failed SQL Agent jobs, backup ages, disk usage, and file used size. It highlights in red anything that needs attention, such as files that need to be manually grown or databases that haven’t been backed up recently. I am not great at powershell, but I was able to find some examples online and modify them to work with my current environment.

One personal goal I have for the upcoming year is to find more ways to use powershell to increase my efficiency. In order to begin doing that, I’ve started to practice using powershell. As I mentioned in my previous post, I’m using http://adventofcode.com/ to help with this challenge.
The second day’s challenge involves a list of dimensions of boxes that need to be wrapped up and tied with a bow by Santa’s elves. Given the list, which contains 1,000 length X width X height measurements, I need to get the total amount of feet needed where each needs the surface area of each box plus a little extra slack, the area of the smallest size. I also needed to get the ribbon around the gift, which was the smallest perimeter around the box plus a litle extra for bow, equal to the cubic volume of the box. I learned some basic commands in Powershell that I either hadn’t learned or had forgotten, such as how to ingest a text file into a variable and how to split an array. In the end, it took a few tries but I got the answer correct for both parts of the question. Below is my final code for each.

$dimensions = get-content N:\adventday2input1.txt
$feet = 0
foreach ($dim in $dimensions) {
[int]$l,[int]$w,[int]$h = $dim.split('x',3)
$feet = $feet + $l*$w*2
$feet = $feet + $l*$h*2
$feet = $feet + $h*$w*2
$intmin = [math]::min($l*$w,$l*$h)
$feet = $feet + [math]::min($intmin,$w*$h)
}
$feet
$dimensions = get-content N:\adventday2input1.txt
$feet = 0
foreach ($dim in $dimensions) {
[int]$l,[int]$w,[int]$h = $dim.split('x',3)
$intmin = [math]::min(2*$l+ 2*$w,2*$l+2*$h)
$feet = $feet + [math]::min($intmin,2*$w+2*$h)
$feet = $feet + $l*$w*$h
}
$feet

In summary, Powershell is a great tool and I’m looking forward to increasing my use of it as 2016 progresses.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s