/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell : WMI Support in RC2 : Privileges and c... "," PowerShell : WMI Support in RC2 (Series part 2) "," PowerShell : WMI Support in RC2 (Series part 1) "," PowerShell RC2 and Active Directory Part 2 "," PowerShell : Hosting IronPython "," PowerShell RC2 and Active Directory "," PowerShell : Using IronPython to Connect to AD and... "," PowerShell Session video of Bruce Payette "," PowerShell : Active Directory Part 11 - moving - R... "," PowerShell : Learn about the HashTable Object and ... "," ")

Thursday, October 12, 2006

 


PowerShell : Calendar Function (GUI)



I often have a need for a quick Calendar  to look up some dates or Week numbers ,

you can double click the clock in the systray to do a quick lookup (I often used it for that) but you need to be an Admin to do so, so it is not available on my own workstation using my normal account , also it shows no week numbers and I often need those  also

So this trick did help me out on several occasions, but still this is not a really ideal solution.

Hence I made this small PowerShell function that does show a Calendar with WeekNumbers in a Form that closes again on hitting [Enter],  that I load in my profile and alias to Cal to keep it ready for a quick lookup of some dates, as you will be surprised how often it comes handy.

(note the dot-space to load in global scope, and the loading forms library in profile (needed for function) 

to test you can also just past the code into the PowerShell console to load the function, and start using the function:

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

. c:\PowerShell\Functions\GetCalendar.ps1 

and here is the script  : (formatted by the html export function from PowerShell Analyzer a great PowerShell editor, if you not know it yet be sure to try it out)

# Function Get-Calendar
#
Shows a calendar with WeekNumbers in a Form
#
/\/\o\/\/ 2006
#
www.ThePowerShellGuy.com

Function
get-Calendar {

$form = new-object Windows.Forms.Form
$form.text = "Calendar"
$form.Size = new-object Drawing.Size @(656,639)

# Make "Hidden" SelectButton to handle Enter Key

$btnSelect = new-object System.Windows.Forms.Button
$btnSelect.Size = "1,1"
$btnSelect.add_Click({
$form.close()
})
$form.Controls.Add($btnSelect )
$form.AcceptButton = $btnSelect

# Add Calendar

$cal = new-object System.Windows.Forms.MonthCalendar
$cal.ShowWeekNumbers = $true
$cal.MaxSelectionCount = 356
$cal.Dock = 'Fill'
$form.Controls.Add($cal)

# Show Form

$Form.Add_Shown({$form.Activate()})
[void]
$form.showdialog()

# Return Start and end date

return $cal.SelectionRange
}

set-alias cal get-Calendar

 



* Update *As the PSA formatting if the Script gives formatting and pasting problems, I'm looking into this, I also will place the script here in the "old" formatting

# Function Get-Calendar
# Shows a calendar with WeekNumbers in a Form
# /\/\o\/\/ 2006
# www.ThePowerShellGuy.com

Function get-Calendar { 

  $form = new-object Windows.Forms.Form 
  $form.text = "Calendar" 
  $form.Size = new-object Drawing.Size @(656,639

  # Make "Hidden" SelectButton to handle Enter Key

  $btnSelect = new-object System.Windows.Forms.Button
  $btnSelect.Size = "1,1"
  $btnSelect.add_Click({ 
    $form.close() 
  }) 
  $form.Controls.Add($btnSelect ) 
  $form.AcceptButton =  $btnSelect

  # Add Calendar 

  $cal = new-object System.Windows.Forms.MonthCalendar 
  $cal.ShowWeekNumbers = $true 
  $cal.MaxSelectionCount = 356
  $cal.Dock = 'Fill' 
  $form.Controls.Add($cal) 

  # Show Form

  $Form.Add_Shown({$form.Activate()})  
  [void]$form.showdialog() 

  # Return Start and end date 

  return $cal.SelectionRange


set-alias cal get-Calendar


So if you are need a quick calendar, you just need ro do a quick cal[enter], in the PowerShell console ..and there you go :


 



PoSH>cal

End Start
--- -----
10/12/2006 12:00:00 AM 10/12/2006 12:00:00 AM


 


 and you can select a timeframe in the calendar to return.


I added a "hidden" button (size 1 by 1) , to handle the [enter] key to close the Form and return the start and end date, to make the calendar more handy to use, as a quick enter will close the form after use and you do not need to use  X to close the form, and I still can use fill on the Calendar control. also you can select a range of days in the Calendar and the function will return the Start and the End date of that period to the PowerShell console when it is closed.


 



PoSH>$vacation = cal
PoSH>$vacation.end

Sunday, November 05, 2006 12:00:00 AM

PoSH>$vacation.start

Saturday, October 14, 2006 12:00:00 AM

PoSH>$vacation.end - $vacation.start

Days : 22
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 19008000000000
TotalDays : 22
TotalHours : 528
TotalMinutes : 31680
TotalSeconds : 1900800
TotalMilliseconds : 1900800000



Enjoy,

Greetings, /\/\o\/\/

Tags : Monad PowerShell







Comments:
Blogger jv
Great!

You are beating us all to the fun stuff again.

I have looked high and low for a blog tool that would habndle code display and clipboard copying. Every one has it's own set of problems.

For some reason teh rendering is pretty bad for this control. Screen desn't repaint correctly when I drag a selection. Have to check to see if it is my desktop settings or an anomoly of NET 2.0 control class.
 
Anonymous Anonymous
I get this error when trying to run it. Any idea what causing the problem?

New-Object : Cannot find type [Windows.Forms.Form]: make sure the assembly containing this type is loaded.
At D:\scripts\powershell\Function\GetCalendar.ps1:8 char:21
 
Blogger /\/\o\/\/
You need to load the forms library (see bold lines above script)

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

greetings /\/\o\/\/
 
Anonymous Roman Kuzmin
This is excellent. From now on it is in my arsenal. Nice trick with a hidden button; you may also use an alternative way if you like it:

$form.KeyPreview = $true
$form.Add_KeyDown({if ($_.KeyCode -eq 'Enter') {$form.Close()}})
 
Anonymous Roman Kuzmin
I was inspired to write a bit more complex script based of your idea because I needed more customization for my tasks. I hope you don't mind that I post the script Get-Calendar.ps1 here: http://nightroman.spaces.live.com/blog/cns!F011223B604739FA!126.entry
Perhaps somebody will find it useful, too.

Thanks,
Roman
 
Anonymous Jim Holbach
Nice function!

I'm a lazy typist so I extended the System.Windows.Forms.SelectionRange type by adding two script properties. The first is named "TimeSpan" and has a GetScriptBlock of "$this.end - $this.start". The second is named "Days" and has a GetScriptBlock of
"($this.end - $this.start).TotalDays"

So I can do

PS> $vacation = cal
PS> $vacation.days
7
PS> $vacation.timespan
Days : 7
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 6048000000000
TotalDays : 7
TotalHours : 168
TotalMinutes : 10080
TotalSeconds : 604800
TotalMilliseconds : 604800000
 
Anonymous Anonymous
Thank you, thank you, thank you!
I have tried in the past to load a form, but have been unsuccessful. Now I can use forms with PowerShell.
 
Blogger /\/\o\/\/
your welcome

enjoy
Greetings /\/\o\/\/
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?