Archive for October, 2006
Get-Calendar.ps1 – Shows a month calendar control and returns selected dates
Posted by Roman Kuzmin in PowerShell on October 15, 2006
The idea is borrowed from the //o//’s blog //o// PowerShelled: Calendar Function (GUI). It is nice and simple. Perhaps a few touches here make it more customizable and useful for wider range of tasks.
Synopsis
The script works as date(s) viewer/picker. It shows WinForm dialog with a month calendar control with predefined date(s) selected and returns start and end date of initial or changed date range. By default 12 months (3 columns, 4 rows) are shown. Week numbers may be optionally turned on by a switch.
Examples
# Select dates and get number of days including the end date $dates = Get-Calendar ($dates[1] - $dates[0]).TotalDays + 1 # Use predefined range, two months view and week numbers shown $start, $end = Get-Calendar '2006.10.16' '2006.11.16' 2,1 -week Write-Host "Start: $start. End: $end."
Get-Calendar.ps1
##
## Author : Roman Kuzmin inspired by //o// www.ThePowerShellGuy.com
## Synopsis : Shows a month calendar control and returns selected dates
## Modified : 2006.10.15
##
## Returns an array of two selected start and end dates
## -Start <datetime>: initial start date
## -End <datetime>: initial end date
## -Dimensions <int[]>: (columns, rows) of months
## -WeekNumbers <switch>: show week numbers
##
## *) Press Enter to return currently selected dates.
## *) Press Escape or close the form to return initial dates.
##
param
(
[datetime]$Start = [datetime]::Now,
[datetime]$End = $Start,
[int[]]$Dimensions = (3, 4),
[switch]$WeekNumbers
)
# load forms
& {[void][System.Windows.Forms.Form]; trap {[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); continue}}
# new calendar
$cal = New-Object System.Windows.Forms.MonthCalendar
$cal.CalendarDimensions = New-Object System.Drawing.Size $Dimensions
$cal.Margin = New-Object System.Windows.Forms.Padding 0
$cal.MaxSelectionCount = 366
$cal.SelectionRange = New-Object System.Windows.Forms.SelectionRange $Start, $End
$cal.ShowWeekNumbers = $WeekNumbers
# new form
$form = New-Object System.Windows.Forms.Form
$form.AutoSize = $true
$form.AutoSizeMode = 'GrowAndShrink'
$form.FormBorderStyle = 'FixedDialog'
$form.KeyPreview = $true
$form.MaximizeBox = $false
$form.Text = 'Calendar'
$form.Controls.Add($cal)
$form.add_Shown({$form.Activate()})
$form.add_KeyDown({
switch($_.KeyCode) {
'Escape' {$form.Close()}
'Return' {$form.Close(); $Start = $cal.SelectionRange.Start; $End = $cal.SelectionRange.End}
}
})
# show and return dates
[void]$form.ShowDialog()
$Start.Date, $End.Date
Get-Choice.ps1 – Displays PowerShell style menu and gets a user choice
Posted by Roman Kuzmin in PowerShell on October 13, 2006
##
## Author : Roman Kuzmin
## Synopsis : Displays PowerShell style menu and gets a user choice
## Modified : 2006.10.13
##
## Returns a choice index
## -caption <string>: menu caption
## -message <string>: menu message
## -choices <string[]>: pairs: item1, help1, item2, help2, ...
## -defaultChoice <int>: default choice index
##
## *) Choice keys are indicated by '&' in menu items.
##
param
(
[string]$caption = 'Confirm',
[string]$message = 'Are you sure you want to continue?',
[string[]]$choices = ('&Yes', 'Continue', '&No', 'Stop'),
[int]$defaultChoice = 0
)
$choiceDescriptions = @()
for($i = 0; $i -lt $choices.Count; $i += 2)
{
$c = [System.Management.Automation.Host.ChoiceDescription]$choices[$i]
$c.HelpMessage = $choices[$i + 1]
$choiceDescriptions += $c
}
$Host.UI.PromptForChoice($caption, $message, [System.Management.Automation.Host.ChoiceDescription[]]$choiceDescriptions, $defaultChoice)
EDIT: See also the modified version for multiple choices here.
Recent Comments