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:
Two lessons from this script are:# 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"
)
- 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.
- 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"
$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
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.