{"id":16651,"date":"2010-10-31T00:01:00","date_gmt":"2010-10-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/31\/weekend-scripter-draw-a-cat-on-a-fence-watching-a-yellow-moon-using-windows-powershell\/"},"modified":"2010-10-31T00:01:00","modified_gmt":"2010-10-31T00:01:00","slug":"weekend-scripter-draw-a-cat-on-a-fence-watching-a-yellow-moon-using-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-draw-a-cat-on-a-fence-watching-a-yellow-moon-using-windows-powershell\/","title":{"rendered":"Weekend Scripter: Draw a Cat on a Fence Watching a Yellow Moon Using Windows PowerShell"},"content":{"rendered":"<p>&nbsp;&nbsp;<\/p>\n<p><b>Summary:<\/b> Learn how to draw a cat on a fence watching a yellow moon by using Windows PowerShell<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. It is seven in the morning yet it seems like it could be four in the morning. It is dark outside, and the room is cool with a hint of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Humidity\">humidity<\/a>. I hang on to my cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/English_Breakfast_tea\">English Breakfast tea<\/a> like a starved dog protects a bone. A soft bubbling sound emanating from the general direction of the stove assures me that my <a href=\"http:\/\/en.wikipedia.org\/wiki\/Irish_oatmeal\">Irish steel-cut oats<\/a> are continuing their slow progression to a cooked state. <\/p>\n<p>A gentle but persistent breeze blows the leaves across the lawn, and the squirrels chatter with one another in the relentless pursuit of nuts and other edible foodstuffs. On such a morning as this, it seems as if almost anything is possible. On such a morning as this, I wrote my first Windows PowerShell script. On such a morning as this, I completed my first Windows PowerShell book. On such a morning as this, I had a cup of English Breakfast tea, waited for my Irish steel-cut oats to cook and wondered about such a morning as this.<\/p>\n<p>While the oats are cooking, I decided to open up Windows PowerShell on the kitchen computer and play around a bit. The <b>DrawCircleWithCatAndMoon.ps1<\/b> script is the result. <\/p>\n<p><strong>DrawCircleWithCatAndMoon.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Function Test-IsIseHost<br \/>{<br \/>&nbsp;&lt;#<br \/>&nbsp;.Synopsis<br \/>&nbsp;Determines if you are running in the Windows PowerShell ISE<br \/>&nbsp;.Description<br \/>&nbsp;This function determines if you are running in the Windows Powershell<br \/>&nbsp;ISE by querying the $ExecutionContext automatic variable.<br \/>&nbsp;.Example<br \/>&nbsp;Test-IsISEHost<br \/>&nbsp;Prints out True if runing inside the ISE, False if run in console<br \/>&nbsp;.Example<br \/>&nbsp;if(Test-IsISEHost) { &#8220;Using the ISE&#8221; }<br \/>&nbsp;Prints out Using the ISE when run inside the ISE, otherwise nothing<br \/>&nbsp;.Inputs<br \/>&nbsp;None<br \/>&nbsp;.Outputs<br \/>&nbsp;[Boolean]<br \/>&nbsp;.Notes<br \/>&nbsp;Name: Test-ISEHost<br \/>&nbsp;Book: Windows PowerShell Best Practices, Microsoft Press, 2009<br \/>&nbsp;Author: Ed Wilson<br \/>&nbsp;Version: 1.0<br \/>&nbsp;Date: 4\/5\/2009<br \/>&nbsp;.Link<br \/>&nbsp;about_Automatic_variables<br \/>&nbsp;Http:\/\/www.ScriptingGuys.Com<br \/>&nbsp;#&gt;<br \/>&nbsp;$ExecutionContext.Host.name -match &#8220;ISE Host$&#8221;<br \/>} #end Test-IsIseHost<\/p>\n<p>Function Set-Circle<br \/>{<br \/>&nbsp;Param(<br \/>&nbsp; [int]$script:xCenter = 20,<br \/>&nbsp; [int]$script:yCenter = 10,<br \/>&nbsp; [int]$script:radius = 5<br \/>&nbsp;)<br \/>&nbsp;Clear-Host<br \/>&nbsp;$r2 = $radius * $radius<br \/>&nbsp;for($x = -$radius; $x -le $radius ; $x++)<br \/>&nbsp; {<br \/>&nbsp;&nbsp; $y = ([int][math]::sqrt($r2 &#8211; $x*$x) + 0.5)<br \/>&nbsp;&nbsp; Set-ConsolePosition -x ($xCenter + $x) -y ($yCenter + $y)<br \/>&nbsp;&nbsp; Write-Host -ForegroundColor Yellow -NoNewline &#8220;@&#8221;<br \/>&nbsp;&nbsp; Set-ConsolePosition -x ($xCenter + $x) -y ($yCenter &#8211; $y)<br \/>&nbsp;&nbsp; Write-Host -ForegroundColor Yellow -NoNewline &#8220;@&#8221;<br \/>&nbsp; } #end for $x<br \/>} #end function Set-Circle<\/p>\n<p>function global:set-ConsolePosition ([int]$x,[int]$y) {<br \/># Get current cursor position and store away<br \/>$position=$host.ui.rawui.cursorposition<br \/># Store new X and Y Co-ordinates away<br \/>$position.x=$x<br \/>$position.y=$y<br \/># Place modified location back to $HOST<br \/>$host.ui.rawui.cursorposition=$position<br \/>} # end function set-consolePosition<\/p>\n<p>Function Set-Fence<br \/>{<br \/>&nbsp;for($a = 1; $a -le 14; $a++)<br \/>&nbsp;{<br \/>&nbsp; if($a -eq 14) <br \/>&nbsp;&nbsp; { Write-host &#8220;_\/\\_&#8221; -nonewline }<br \/>&nbsp; else<br \/>&nbsp;&nbsp; { Write-host &#8220;_\/\\&#8221; -nonewline }<br \/>&nbsp; }<br \/>&nbsp;&#8220;&#8221;<br \/>for($i = 1 ; $i -le 5 ; $i ++)<br \/>&nbsp;{ <br \/>&nbsp;&nbsp; for($j = 1 ; $j -le 15 ; $j++)<br \/>&nbsp;&nbsp;&nbsp; { write-host &#8220;|&nbsp; &#8221; -nonewline }<br \/>&nbsp;&nbsp;&nbsp; &#8220;&#8221;<br \/>&nbsp;}<br \/>} #End function Set-Fence<\/p>\n<p>Function Set-Cat<br \/>{<br \/>$cat=@&#8221;<br \/>&nbsp;\/\\___\/\\<br \/>&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp; (<br \/>=\\&nbsp;&nbsp;&nbsp;&nbsp; \/=<br \/>&nbsp; }==={<br \/>&nbsp;\/&nbsp;&nbsp;&nbsp;&nbsp; \\<br \/>&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp; |<br \/>\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \\<br \/>\\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/<br \/>&nbsp;\\__&nbsp; _\/<br \/>&nbsp;&nbsp;&nbsp; ( <br \/>&nbsp;&nbsp;&nbsp;&nbsp; ) <br \/>&nbsp;&nbsp;&nbsp; *<br \/>&#8220;@ Write-host -ForegroundColor Green $cat -noNewLine<br \/>} #end function Set-Cat<\/p>\n<p># *** Entry point to Script ***<\/p>\n<p>if(Test-isIseHost) {&#8220;this script does not run in the ISE&#8221; ; exit}<br \/>$Host.UI.RawUI.WindowTitle = &#8220;Scripting Guys say hi&#8230;&#8221;<br \/>Set-Circle -xCenter 20 -yCenter 10 -radius 5<br \/>Set-ConsolePosition -x 0 -y ($yCenter + $Radius +19)<br \/>Set-Fence<br \/>Set-ConsolePosition -x 0 -y ($yCenter + $Radius +10)<br \/>Set-Cat<br \/>Set-ConsolePosition -x 0 -y ($yCenter + $Radius +25)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>This script will not run correctly in the Windows PowerShell ISE. Therefore, I use the <b>Test-isIseHost<\/b> function to check the host that is executing the script. If the script is run inside the Windows PowerShell ISE, the script will exit. Most of the function is taken up with comment-based help but the essential line of code is seen here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">$ExecutionContext.Host.name -match &#8220;ISE Host$&#8221;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The script begins by creating the Set-Circle function. This function accepts three input parameters that determine where the circle will be drawn and the size of the circle to draw. The Set-Circle function is seen here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Function Set-Circle<br \/>{<br \/>&nbsp;Param(<br \/>&nbsp; [int]$script:xCenter = 20,<br \/>&nbsp; [int]$script:yCenter = 10,<br \/>&nbsp; [int]$script:radius = 5<br \/>&nbsp;)<br \/>&nbsp;Clear-Host<br \/>&nbsp;$r2 = $radius * $radius<br \/>&nbsp;for($x = -$radius; $x -le $radius ; $x++)<br \/>&nbsp; {<br \/>&nbsp;&nbsp; $y = ([int][math]::sqrt($r2 &#8211; $x*$x) + 0.5)<br \/>&nbsp;&nbsp; Set-ConsolePosition -x ($xCenter + $x) -y ($yCenter + $y)<br \/>&nbsp;&nbsp; Write-Host -ForegroundColor Yellow -NoNewline &#8220;@&#8221;<br \/>&nbsp;&nbsp; Set-ConsolePosition -x ($xCenter + $x) -y ($yCenter &#8211; $y)<br \/>&nbsp;&nbsp; Write-Host -ForegroundColor Yellow -NoNewline &#8220;@&#8221;<br \/>&nbsp; } #end for $x<br \/>} #end function Set-Circle<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The <b>Set-Circle<\/b> function calls the <b>Set-ConsolePosition<\/b> function to set the insertion point in the Windows PowerShell console. Then it uses the <b>Write-Host<\/b> Windows PowerShell cmdlet to write the at sign character (@) to the Windows PowerShell console. The <b>Set-ConsolePosition<\/b> function was written by Microsoft MVP Sean Kearney. This function is discussed in the &#8220;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/19\/draw-boxes-and-lines-in-the-windows-powershell-console-host.aspx\">Draw Boxes and Lines in the Windows PowerShell Console Host<\/a>&#8221; <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/\">Hey Scripting Guy! blog<\/a>. It was also used in the &#8220;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/26\/modify-the-powershell-console-title-and-display-a-rabbit.aspx\">Modify the PowerShell Console Title and Display a Rabbit<\/a>&#8221; Weekend Scripter post that I wrote for the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\">Scripting Wife<\/a>&#8216;s birthday. The <b>Set-ConsolePosition<\/b> function is seen here. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">function global:Set-ConsolePosition ([int]$x,[int]$y) {<br \/># Get current cursor position and store away<br \/>$position=$host.ui.rawui.cursorposition<br \/># Store new X and Y Co-ordinates away<br \/>$position.x=$x<br \/>$position.y=$y<br \/># Place modified location back to $HOST<br \/>$host.ui.rawui.cursorposition=$position<br \/>} # end function set-consolePosition<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The <b>Set-Fence<\/b> function is used to draw the fence to the Windows PowerShell console. Because there is lots of repetition in building a fence (whether in real life (IRL) or virtually, it is a natural fit for a <i>for<\/i> loop. I decided to use the <i>if<\/i> statement to detect when I am coming to the end of the fence so that I could add the last &#8220;board&#8221; in the last position. The <b>Set-Fence<\/b> function is seen here. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Function Set-Fence<br \/>{<br \/>&nbsp;for($a = 1; $a -le 14; $a++)<br \/>&nbsp;{<br \/>&nbsp; if($a -eq 14) <br \/>&nbsp;&nbsp; { Write-host &#8220;_\/\\_&#8221; -nonewline }<br \/>&nbsp; else<br \/>&nbsp;&nbsp; { Write-host &#8220;_\/\\&#8221; -nonewline }<br \/>&nbsp; }<br \/>&nbsp;&#8220;&#8221;<br \/>for($i = 1 ; $i -le 5 ; $i ++)<br \/>&nbsp;{ <br \/>&nbsp;&nbsp; for($j = 1 ; $j -le 15 ; $j++)<br \/>&nbsp;&nbsp;&nbsp; { write-host &#8220;|&nbsp; &#8221; -nonewline }<br \/>&nbsp;&nbsp;&nbsp; &#8220;&#8221;<br \/>&nbsp;}<br \/>} #End function Set-Fence<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p>The <b>Set-Cat<\/b> function uses a <b>here-string<\/b> to define a cat that is actually drawn to the Windows PowerShell console through the <b>Write-Host<\/b> Windows PowerShell cmdlet. This function is shown here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">Function Set-Cat<br \/>{<br \/>$cat=@&#8221;<br \/>&nbsp;\/\\___\/\\<br \/>&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp; (<br \/>=\\&nbsp;&nbsp;&nbsp;&nbsp; \/=<br \/>&nbsp; }==={<br \/>&nbsp;\/&nbsp;&nbsp;&nbsp;&nbsp; \\<br \/>&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp; |<br \/>\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \\<br \/>\\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/<br \/>&nbsp;\\__&nbsp; _\/<br \/>&nbsp;&nbsp;&nbsp; ( <br \/>&nbsp;&nbsp;&nbsp;&nbsp; ) <br \/>&nbsp;&nbsp;&nbsp; *<br \/>&#8220;@ <br \/>Write-host -ForegroundColor Green $cat -noNewLine<br \/>} #end function Set-Cat<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>The entry point to the script puts everything together. I use the window <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb230668(v=office.12).aspx\">title property<\/a> from the <b>InternalHostRawUserInterface<\/b> class to change the default Windows PowerShell console Window title. The <b>InternalHostRawUserInterface<\/b> class is located in the <b>System.Management.Automation.Internal.Host<\/b> .NET Framework namespace, and is documented in the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee156814.aspx\">Windows PowerShell Owner&#8217;s Manual<\/a>. The entry point to the script is seen here.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\"># *** Entry point to Script ***<\/p>\n<p>$Host.UI.RawUI.WindowTitle = &#8220;Scripting Guys say hi&#8230;&#8221;<br \/>Set-Circle -xCenter 20 -yCenter 10 -radius 5<br \/>Set-ConsolePosition -x 0 -y ($yCenter + $Radius +19)<br \/>Set-Fence<br \/>Set-ConsolePosition -x 0 -y ($yCenter + $Radius +10)<br \/>Set-Cat<br \/>Set-ConsolePosition -x 0 -y ($yCenter + $Radius +25)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/p>\n<p>When the script runs, the output seen in the following figure appears.<\/p>\n<p>&nbsp;<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1030.WES-10-31-10-01.jpg\" border=\"0\" \/>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum.<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp; Summary: Learn how to draw a cat on a fence watching a yellow moon by using Windows PowerShell &nbsp; Microsoft Scripting Guy Ed Wilson here. It is seven in the morning yet it seems like it could be four in the morning. It is dark outside, and the room is cool with a hint [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25,3,4,61,45,100],"class_list":["post-16651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-windows-powershell-ise"],"acf":[],"blog_post_summary":"<p>&nbsp;&nbsp; Summary: Learn how to draw a cat on a fence watching a yellow moon by using Windows PowerShell &nbsp; Microsoft Scripting Guy Ed Wilson here. It is seven in the morning yet it seems like it could be four in the morning. It is dark outside, and the room is cool with a hint [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16651"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}