Get-Calendar2.ps1
#===============================================================================
# Get-Calendar2: 指定した年月のカレンダーを表示する(色つき版)
#
# パラメータ:
# $Year: 年(省略した場合は現在の年)
# $Month: 月(省略した場合は現在の月)
#
# 使用例1(年月指定)
# PS > Get-Calendar2 2008 2
#
# 使用例2(年月省略)
# PS > Get-Calendar
#
# copyright HIRO's.NET(http://hiros-dot.net//)
#===============================================================================
function global:Get-Calendar2
{
Param ([int]$Year = $(Get-Date).Year, [int]$Month = $(Get-Date).Month)
#表示用年月作成
$DispMonth = New-Object DateTime($Year, $Month, 1)
$strDispMonth = ($DispMonth.ToString("MMMM",[System.Globalization.CultureInfo]'en-US') + " " + $Year)
#横幅35文字の中間に表示されるようにする
Write-Host ""
Write-Host $strDispMonth.PadLeft([int](35 - (35 - $strDispMonth.Length) / 2))
$firstSunday = Get-FirstSunday $Year $Month
$lastDay = Get-LastDay $Year $Month
Write-Host "Sun." -foregroundcolor Red -noNewline
Write-Host " Mon. Tue. Wed. Thu. Fri. " -noNewline
Write-Host "Sat." -foregroundcolor Cyan
# 第1日曜日が"1日"以外の第1週を表示
if ( $firstSunday -ne 1 )
{
for ( $i = 1; $i -lt $firstSunday - 1; $i++ )
{
$week += $i.ToString(" # ")
}
$spc = " "
$spc *= [int](7 - ( $firstSunday - 1 ))
Write-Host ($spc + $week) -noNewline
#土曜日をCyanにする
Write-Host ($firstSunday - 1).ToString(" # ") -foregroundcolor Cyan
}
#中間週を表示
$loopWeek = [Math]::floor(($LastDay - $firstSunday) / 7)
$Day = $firstSunday
for ( $weekCnt = 0; $weekCnt -lt $loopWeek; $weekCnt++ )
{
for ( $iDay = 0; $iDay -lt 7; $iDay++ )
{
if ( $Day -lt 10 ) { $strDay = $Day.ToString(" # ") }
else { $strDay = $Day.ToString(" ## ") }
#日曜日はRed, 土曜日はCyanにする
if ($iDay -eq 0 ) { Write-Host $strDay -foregroundcolor Red -noNewline }
elseif ($iDay -eq 6 ) { Write-Host $strDay -foregroundcolor Cyan}
else { Write-Host $strDay -noNewline }
$Day += 1
}
#Write-Host ($week)
}
#最終週を表示
if ( $Day -le $LastDay )
{
$week = ""
$LastSunday = $Day
$Day += 1
for ( $iDay = $Day; $iDay -le $LastDay - 1; $iDay++ )
{
$week += $Day.ToString(" ## ")
$Day += 1
}
#最終週の日曜日の色をRedにする
Write-Host $LastSunday.ToString(" ## ") -foregroundcolor Red -noNewline
Write-Host $week -noNewline
#最終日が土曜日の場合は色をCyanにする
if ($LastDay - $LastSunday -eq 6) { Write-Host $LastDay.ToString(" ## ") -foregroundcolor Cyan }
else { Write-Host $LastDay.ToString(" ## ") }
}
}