Time till we land …

PowerShell Team

PowerShell Team

<Today we have a guest blog entry from Ed Wilson – Author of the upcoming MSPRESS book, Microsoft Windows PowerShell Step By Step>

<2/16 fixed a bug pointed out by Richard Siddaway [use TotalMinutes instead of Minutes for the out-gauge].  Thanks Richard! -jps>

I was on my way to Ottawa this week, and working on a few scripts, when I realized that perhaps I might need to keep track of time. My poor old combination laptop/coffee warmer is struggling to keep up the pace, and as a result I need a good five minute warning to shut the silly thing down prior to landing. The flight attendants do not usually give me sufficient warning to put the beast to bed. Alas, what’s a consultant to do…?

I decided to use the New-TimeSpan cmdlet to calculate the difference between the current time (obtained by get-date) and the time the plane would land at 12:55 pm. So I came up with this “one-liner”…

New-TimeSpan -Start (Get-Date) -End (Get-Date -Hour 12 -Minute 55)

After running the command, I was able to immediately see how long I had until the plane landed as shown here…

Days : 0

Hours : 0

Minutes : 56

Seconds : 0

Milliseconds : 15

Ticks : 33600156250

TotalDays : 0.0388890697337963

TotalHours : 0.933337673611111

TotalMinutes : 56.0002604166667

TotalSeconds : 3360.015625

TotalMilliseconds : 3360015.625

The problem, is this is way too reading. So I pipelined the output to the Format-List cmdlet. The revised code is seen here.

New-TimeSpan -Start (Get-Date) -End (Get-Date -Hour 12 -Minute 55) | Format-List TotalMinutes

Now the output is simple: TotalMinutes : 54

All I have to do now, is up-arrow, and press enter, and I have a clear idea of when the plane will land. But wait, there’s more. I really did not want to have to remember to up arrow, all the time… dude, if I was going to remember that, I could probably remember to look at the clock on my coffee warmer and see that…. So I added in the PowerGadgets snap-in as shown here (See Jeffrey’s earlier blog on PowerGadgets… they rock!)

Add-PSSnapin PowerGadgets

Up arrow … remove the Format-List cmdlet, replace with Out-gage. Specify the value is minutes, and I would like it to float and refresh every 30 seconds. Set the max scale for 60 minutes and voila …

New-TimeSpan -Start (Get-Date) -End (Get-Date -Hour 22 -Minute 00) |

Out-Gauge -Value TotalMinutes -Floating -Refresh 0:0:30 -MainScale_max 60

Now I get a floating gage that I can move around as needed. It is measured for 60 minutes, and will automatically refresh every 30 seconds. And it only took one line of code… cool.

Ed Wilson [MSFT]

US Enterprise Services, Consulting for IT Operations

Microsoft Windows PowerShell Step By Step, Microsoft Press (Forthcoming)


PowerShell Team
PowerShell Team

Follow PowerShell Team   

No Comments.