Wednesday, 25 May 2011

Using New-TimeSpan cmdlet in scripts

During the 2011 Scripting Games I learned to used many new things... one being how to use New-TimeSpan cmdlet.

It was April 12th 2011 on and Event 7 of the scripting games was posted... Our challenge?  Create a pop up box at log on to show how long is left to submit orders before the financial year ends... Read more on the following page: http://blogs.technet.com/b/heyscriptingguy/archive/tags/2011+scripting+games/beginner/event+7/

My solution to this was as follows:

# Scripting Games 2011 Beginner Event 7 #
$Timespan = New-TimeSpan -Start (get-date) -End 31/07/2011
$MsgBox = New-Object -ComObject wscript.shell
$MsgBox.Popup("There are $($timespan.days) days until the end of the fiscal year.",0,"Fiscal Year Notification")
Two lessons from this script are:
  1. Making pop up message boxes.  This is very simple way using the New-Object cmdlet to create the box with a ComObject.  When I say this is easy, I mean easy to use not explain.  Resources like the Scripting Guy or MSDN will help explain this much better than I could.
  2. New-TimeSpan cmdlet.  This is what I wanted to explain in this post.  Basically if you want to see the difference in time between one date and another then it's simple using this cmdlet.  For example if you want to know how many days there are between November 10th 2012 and December 11th 2013 you would simply type "New-TimeSpan 10/11/12 11/12/13" (For the UK US "New-TimeSpan 11/10/12 12/11/13" should work).  This will give the result of "Days : 296"
In the Scripting games I was only required to give a count down of days but this can be extended to a specific point in the day.  The following block of script is my countdown to when I finish working at my current employer.  I have the TimeSpan going from Now (Using Get-Date to retrieve current date and time) and then I use Get-Date but with parameters to specify the Day, Month, Year, Hour, and Minute of the day.  I have put the cmdlet and it's parameters into a Variable to make it easier to specify later in the script when I want only, for example, the number of days, or hours.


$Timespan = New-TimeSpan -Start (get-date) -End (Get-Date -Day 24 -Month 6 -Year 2011 -Hour 15 -Minute 30)
$MsgBox = New-Object -ComObject wscript.shell
$MsgBox.Popup("There are $($timespan.days) days, $($timespan.hours) hours, and $($timespan.minutes) minutes until I Finish!",0,"Time Until I Leave")


I have made 2 variables in this script; one for the TimeSpan, and the other for the message box.  With both of these I extend them by using some of their methods.  To find out what their methods are Create the variable (The following example will use the Message Box) "$MsgBox = New-Object -ComObject wscript.shell", then type "$MsgBox | Get-Member" you will see all methods available to that object.  From this list, as I wanted a pop up box, I selected the Pop up method.  This was used by typing "$MsgBox.Popup("Text String for in the box",0,"Window Title")".  From what I can tell the "0" means it waits for a response by clicking the OK button.  If you put a different number then it will automatically shut after is the number of seconds.

With the $TimeSpan variable I have used the object properties to only display the property I am wanting.  For example to show only the Days property I just use $TimeSpan.Days and it will display the number of days.


For this script I have saved it and created a shortcut on my desktop to the following:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "c:\Scripts\TimeUntilDeparture.ps1"

I hope this is of use to you...
Thanks for reading.

No comments:

Post a Comment