<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Calendar</title>
	<atom:link href="http://huddledmasses.org/tag/calendar/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>2008 Scripting Games &#8211; Solution for Advanced Event 4</title>
		<link>http://huddledmasses.org/2008-scripting-games-solution-for-advanced-event-4/</link>
		<comments>http://huddledmasses.org/2008-scripting-games-solution-for-advanced-event-4/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 22:17:17 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[2008 Scripting Games]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/2008-scripting-games-solution-for-advanced-event-4/</guid>
		<description><![CDATA[This continues my series of solution posts for the 2008 Scripting Games with my solution for the Advanced Event 4 which was about drawing a &#8220;graphical&#8221; calendar in the console. Basically, this is a simple task, and the only complication was the requirement to allow passing in the month. There are solutions from the Scripting [...]]]></description>
			<content:encoded><![CDATA[	<p>This continues my series of solution posts for the <a href="http://HuddledMasses.org/tag/2008-scripting-games/">2008 Scripting Games</a> with my solution for the <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent4.mspx">Advanced Event 4</a> which was about drawing a &#8220;graphical&#8221; calendar in the console. Basically, this is a simple task, and the only complication was the requirement to allow passing in the month.</p>

	<p>There are solutions from <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/apssol04.mspx">the Scripting Guys</a> and <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/expssol03.mspx">Ed Wilson</a> already available, and both of them are pretty good solutions, so I would have left this post in the draft bin if it wasn&#8217;t for the fact that I <a href="http://huddledmasses.org/scripting-games-2008-part-1/">already showed off screenshots</a> of my final script (which is a bit over the top).  Because I wrote such a complicated script in the end, I wanted to show you how it started before I show you how it ended up:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$now</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Please enter a date&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;"># make sure that $now is the first day of the month:</span><br />
<span style="color: #660033; font-weight: bold;">$now</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">AddDays</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Write the month string at the top</span><br />
<span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &nbsp; MMMM, yyyy &nbsp; &quot;</span><span style="color: #333;">&#41;</span> <br />
<br />
<span style="color: #666666; font-style: italic;"># Write the day names below it</span><br />
<span style="color: #009900;">&quot; Su Mo Tu We Th Fr Sa &quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Figure out the day of the week for the first day of the month</span><br />
<span style="color: #666666; font-style: italic;"># Note: if you don't cast to [int], this returns the name, like &quot;Monday&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$dow</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">DayOfWeek</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Total number of days ...</span><br />
<span style="color: #660033; font-weight: bold;">$days</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DaysInMonth</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Year</span>, <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Month</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># write the calendar as a single string by joining an array of strings ...</span><br />
<span style="color: #666666; font-style: italic;"># Starting on Sunday before the first day of the month (aka: 1-$dow)</span><br />
<span style="color: #666666; font-style: italic;"># Ending after the number of days in the month ...</span><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;&quot;</span> @<span style="color: #333;">&#40;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span><span style="color: #660033; font-weight: bold;">$dow</span><span style="color: #333;">&#41;</span>..<span style="color: #660033; font-weight: bold;">$days</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-lt</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span> <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot; &nbsp;&quot;</span><span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># -lt 1, just insert padding &nbsp; </span><br />
&nbsp; <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#125;</span> <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;{0,2}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># -gt 0, the number, padded to 2 chars</span><br />
&nbsp; <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">0</span> <span style="color: #000066;">-eq</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$dow</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">7</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># 7 Mod 7, insert a new-line</span></div>

	<p>See that?  Not really very complicated, right?  It&#8217;s just about ten lines of code, and three of them are unnecessary variable assignments which just help make the script more readable (actually, there&#8217;s really only 2 necessary lines<sup class="footnote"><a href="#fn11150454114fbca2eb23298">1</a></sup>).  There&#8217;s a couple of tricks here though: <span id="more-499"></span></p>

	<ol>
		<li>PowerShell has range notation that allows you to create an array of numbers from <code>$x</code> to <code>$y</code> by saying <code>$x..$y</code>, and it works with negative numbers too, so I can iterate from the Sunday (Day of the Week = 0) before the start of the month by doing: <code>(1-$dow)..$days</code></li>
		<li>The PowerShell <code>switch</code> statement has built in <code>foreach</code> support, so if you write <code>switch($array){...}</code>, it&#8217;s basically equivalent to: <code>foreach($_ in $array){ switch($_) {... } }</code> &#8212; pretty neat, right?  </li>
		<li>The PowerShell switch statement allows script blocks as the conditions, so rather than having to switch on integers or characters or even strings, like in C# ... I can switch on script blocks.</li>
	</ol>
	<ol>
		<li>The PowerShell switch statement supports fall-through <strong>with testing</strong> &#8212; unlike the C# switch where you have to explicitly <code>goto case</code>, and unlike the old C++ switch where you would fall through to the next case regardless of the what the case was  ... In PowerShell, if you don&#8217;t use <code>break</code> to break out of the switch, each item will be tested against each case, and every case which matches will be executed.  This allows me to have a case for less than 1 and greater than 0 and know that they exclude each other, but that the last case (testing whether this day is the last day of the week and needs to line-wrap) will always be tested.</li>
	</ol>

	<p>The actual script I submitted does some fancy formatting.  It shows the last few days of the month before and  the first few of the month after (just enough to fill out the week) in a different color,  and also allows you to specify the day of the month.  If you do specify the day of the month, it highlights it, and optionally colors past days in another color (to sort-of highlight the remaining days of the month).  All of the colors are parameters to the script, so you can feel free to take this and have fun integrating it into your startup profile or whatever  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>At it&#8217;s essence, this script is basically the same as the script above, but split into five parts to allow for custom colors:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$date</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Please enter a date&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;"># and optionally, a bunch of color settings ...</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$background</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">BackgroundColor</span> <span style="color: #666666; font-style: italic;"># &quot;DarkBlue&quot;</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$foreground</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">ForegroundColor</span> <span style="color: #666666; font-style: italic;"># &quot;White&quot;</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$othermonths</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;DarkGray&quot;</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$monthname</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Yellow&quot;</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$today</span> &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Green&quot;</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$pastdays</span> &nbsp; &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Gray&quot;</span><br />
,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$dayNames</span> &nbsp; &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Cyan&quot;</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666666; font-style: italic;"># Convert the string to a DateTime ...</span><br />
<span style="color: #666666; font-style: italic;"># We accept it as a string only so that we can ...</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$now</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$date</span>; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span>$?<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">exit</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># Use this silly trick to see if you typed a DAY as well as month (more than one separator)</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">bool</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$highlightDay</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$date</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$date</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;[- ,./]&quot;</span>,<span style="color: #009900;">&quot;&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">1</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Mess with the colors...</span><br />
<span style="color: #660033; font-weight: bold;">$bg</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">BackgroundColor</span><br />
<span style="color: #660033; font-weight: bold;">$fg</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">ForegroundColor</span><br />
<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">BackgroundColor</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$background</span><br />
<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">ForegroundColor</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$foreground</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$dow</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">AddDays</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">DayOfWeek</span><br />
<span style="color: #660033; font-weight: bold;">$days</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DaysInMonth</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Year</span>, <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Month</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Some fancy string-centering and coloring for the headers</span><br />
<span style="color: #660033; font-weight: bold;">$monthString</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;MMMM, yyyy&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$monthPad</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot; &quot;</span><span style="color: #66cc66;">*</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">-</span><span style="color: #660033; font-weight: bold;">$monthString</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span>;<br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #000066;">-back</span> <span style="color: #660033; font-weight: bold;">$background</span><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>$monthPad$monthString$monthPad &quot;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$monthname</span><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span> Su Mo Tu We Th Fr Sa &quot;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$dayNames</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Write out the last few days of last month</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$dow</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$lastMonth</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">AddMonths</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$daysLM</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DaysInMonth</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$lastMonth</span>.<span style="color: #003366;">Year</span>, <span style="color: #660033; font-weight: bold;">$lastMonth</span>.<span style="color: #003366;">Month</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;&quot;</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$daysLM</span><span style="color: #66cc66;">-</span><span style="color: #660033; font-weight: bold;">$dow</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span>..<span style="color: #660033; font-weight: bold;">$daysLM</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;{0,2}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$othermonths</span> <span style="color: #000066;">-nonew</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Then, the days BEFORE today (if there are any) get their own color...</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;&quot;</span> @<span style="color: #333;">&#40;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span>1..<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;{0,2}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> &nbsp;<span style="color: #666666; font-style: italic;"># by default, just the number</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">0</span> <span style="color: #000066;">-eq</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$dow</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">7</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span> &nbsp;<span style="color: #666666; font-style: italic;"># 0 Mod 7, insert a new-line</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$pastdays</span> <span style="color: #000066;">-nonew</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## And of course, &quot;Today&quot; (if specified) gets its own color</span><br />
<span style="color: #666666; font-style: italic;">## and it's padded to 3 characters because it's not part of an array</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$highlightDay</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;{0,3}&quot;</span> <span style="color: #000066;">-f</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$today</span> <span style="color: #000066;">-nonew</span>:$<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">DayOfWeek</span> <span style="color: #000066;">-ne</span> <span style="color: #cc66cc;">6</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;{0,3}&quot;</span> <span style="color: #000066;">-f</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$foreground</span> <span style="color: #000066;">-nonew</span>:$<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">DayOfWeek</span> <span style="color: #000066;">-ne</span> <span style="color: #cc66cc;">6</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## And then, the days AFTER today (if there are any)</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$days</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;&quot;</span> @<span style="color: #333;">&#40;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span>..<span style="color: #660033; font-weight: bold;">$days</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;{0,2}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> &nbsp;<span style="color: #666666; font-style: italic;"># by default, just the number</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><span style="color: #cc66cc;">0</span> <span style="color: #000066;">-eq</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$dow</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">7</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span> &nbsp;<span style="color: #666666; font-style: italic;"># 0 Mod 7, insert a new-line</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$foreground</span> <span style="color: #000066;">-nonew</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## And finally, the first few days of next month (if they fit)</span><br />
<span style="color: #660033; font-weight: bold;">$nextMonth</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">AddDays</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$days</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$now</span>.<span style="color: #003366;">Day</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$nextMonth</span>.<span style="color: #003366;">DayOfWeek</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> &nbsp;<span style="color: #009900;">&quot;&quot;</span> @<span style="color: #333;">&#40;</span>1..<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">-</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$nextMonth</span>.<span style="color: #003366;">DayOfWeek</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|%</span> <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;{0,2}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-fore</span> <span style="color: #660033; font-weight: bold;">$othermonths</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #666666; font-style: italic;"># a trailing line of $background colored space</span><br />
<br />
<span style="color: #666666; font-style: italic;"># set the colors back ...</span><br />
<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">BackgroundColor</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$bg</span><br />
<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">ForegroundColor</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$fg</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #666666; font-style: italic;"># another trailing line of whitespace</span></div>

	<p id="fn11150454114fbca2eb23298" class="footnote"><sup>1</sup> For those wondering about the lines of code &#8230; here&#8217;s a solution that&#8217;s a single PowerShell command:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;&quot;</span> @<span style="color: #333;">&#40;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">AddDays</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">Day</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">DayOfWeek</span><span style="color: #333;">&#41;</span>..<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DaysInMonth</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">now</span>.<span style="color: #003366;">Year</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">now</span>.<span style="color: #003366;">Month</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-eq</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &nbsp; MMMM, yyyy &nbsp; <span style="color: #000099; font-weight: bold;">`n</span> \S\u \M\o \T\u \W\e \T\h \F\r \S\a <span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-lt</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #009900;">&quot; &nbsp;&quot;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;{0,2}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #cc66cc;">0</span> <span style="color: #000066;">-eq</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">+</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">AddDays</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">-</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">Day</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">DayOfWeek</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">7</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span></div>

	<p>Clearly this is <strong>not</strong> good code:</p>

	<ul>
		<li>I&#8217;m recalculating the <code>$dow #Day Of Week</code> variable for each loop iteration, just to avoid a line of code declaring it</li>
		<li>The <code>$x..$y</code> range statement is just hideous and incomprehensible</li>
	</ul>
	<ul>
		<li>I&#8217;ve added a test to the switch statement which is <span class="caps">ONLY</span> ever true the first time through the loop just so I could write out the header without having a separate line outside the switch.</li>
	</ul>

	<p>Technically, it&#8217;s only one line, and has no semicolons &#8230; I think I&#8217;m going to have to amend my definition of Lines Of Code to count each case test and corresponding script block of a switch statement as <strong>at least</strong> one line of code, just to discourage the writing of code like that.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':-p' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/2008-scripting-games-solution-for-advanced-event-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting Games 2008 &#8211; Part 1</title>
		<link>http://huddledmasses.org/scripting-games-2008-part-1/</link>
		<comments>http://huddledmasses.org/scripting-games-2008-part-1/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 03:43:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Scripting Games]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/scripting-games-2008-part-1/</guid>
		<description><![CDATA[I&#8217;ve labeled this part 1 because I&#8217;m planning on writing more about a couple of my solutions to the Microsoft Scripting Games 2008, a few of my solutions are good examples of the power of PowerShell or of scripting languages in .Net, or just neat features. Just for fun, I thought I&#8217;d post a screenshot [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve labeled this part 1 because I&#8217;m planning on writing more about a couple of my solutions to the <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/advanced.mspx">Microsoft Scripting Games 2008</a>, a few of my solutions are good examples of the power of PowerShell or of scripting languages in .Net, or just neat features.</p>

	<p>Just for fun, I thought I&#8217;d post a screenshot of my solution for Event4 because I saw that <a href="http://thepowershellguy.com/blogs/posh/archive/2008/02/16/the-scripting-games-have-started-and-some-teasers.aspx">/\/\o\/\/ was</a>... <img src="http://huddledmasses.org/images/PowerShell/SG08/PowerShellCalendar.jpg" alt="" width="527" height="621" /></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/scripting-games-2008-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

