{"id":78966,"date":"2016-06-23T00:01:03","date_gmt":"2016-06-23T07:01:03","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78966"},"modified":"2019-02-18T09:10:35","modified_gmt":"2019-02-18T16:10:35","slug":"build-a-hexadecimal-clock-in-powershell-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-a-hexadecimal-clock-in-powershell-part-4\/","title":{"rendered":"Build a hexadecimal clock in PowerShell \u2013 Part 4"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use PowerShell to navigate the neutral zone.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here today to continue forth (not COBOL nor even Visual BASIC) into our silly yet fun quest to build a hexadecimal clock by using the PowerShell console.<\/p>\n<p>It does seem silly, doesn\u2019t it? Yet, consider some of the things that we\u2019ve learned in PowerShell during our quest.<\/p>\n<ul>\n<li>Here-String<\/li>\n<li>Defining array<\/li>\n<li>Select-String searches<\/li>\n<li>Convert number bases<\/li>\n<li>Singing songs about huge tracts of land\u2026 (Oh no, wait\u2026. Sorry\u2026 That was a movie I watched last night.)<\/li>\n<\/ul>\n<p>So, although the quest seems silly, we have learned quite a bit.<\/p>\n<p>Actually, it&#8217;s much like any quest. The goal isn\u2019t the point. It\u2019s what you learn or experience while getting there.<\/p>\n<p>Our next part is to figure out how to draw this silly thing. Oh, well, good grief!<\/p>\n<p>I hadn\u2019t <u>really<\/u> thought this one out.<\/p>\n<p>So far, we have a string of characters (Our HexTime).<\/p>\n<p>From this string, we need to identify the members of our array and show them (Giant Letters from Mars).<\/p>\n<p>That part <u>seems<\/u> simple enough. We can just loop through and show the output.<\/p>\n<p style=\"padding-left: 90px\"><code>For ($Count=0; $Count -lt ($HexTime.Length); $Count++)<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>{<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>$Character=($HexArray[('0123456789abcdef\/-' | Select-String -pattern ($HexTime[$Count])).Matches.Index])<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>$Character<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>}<\/code><\/p>\n<p>This is a variant on yesterday\u2019s exercise, but it goes one step further. Access the array from the <strong>Index<\/strong> value and show the relevant character. This actually works if you want a vertical clock.<\/p>\n<p>I really want a horizontal clock, like the one on my desk. To achieve this in the console, I need to figure a way to move the cursor to a unique position on the screen.<\/p>\n<p>We can use the <strong>$host<\/strong> variable, which is a live object in the PowerShell console that contains information such as the color of the screen and cursor position.<\/p>\n<p>With this object, we can see, capture, and, most importantly, <u>set<\/u> the position of the cursor in the console.<\/p>\n<p>Neat, eh? Let\u2019s clear the screen and capture the current location.<\/p>\n<p style=\"padding-left: 90px\"><code>Clear-Host\n<\/code><code>$Position=$host.ui.RawUI.CursorPosition<\/code><\/p>\n<p>If you look at the current object, you\u2019ll see the following results:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78975\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062316.png\" alt=\"Screenshot of the cursor position.\" width=\"358\" height=\"118\" \/><\/a><\/p>\n<p>This tells us that the cursor is at column 0 and row 0 (the very top) after we just wiped the screen.<\/p>\n<p>Now, to modify where the cursor is going to be, we do three things.<\/p>\n<ul>\n<li>Capture the position<\/li>\n<li>Set the X and\/or Y values in the captured object<\/li>\n<li>Set the position from the captured object<\/li>\n<\/ul>\n<p>An example of this in action by using Windows PowerShell (followed by <strong>Write-Host<\/strong> of some output) can be seen in the following example:<\/p>\n<p style=\"padding-left: 90px\"><code>$CursorPosition=$Host.UI.Rawui.CursorPosition\n# Set column to 20\n$CursorPosition.X=20\n# Set row to 15\n$CursorPosition.Y=15\n# Set Cursor Position\n$Host.UI.RawUI.CursorPosition=$CursorPosition<\/code><\/p>\n<p>If we\u2019d like to have each of these characters at a unique position, we\u2019ll need to pick a start point and then adjust the column. My <strong>Here-String<\/strong>s are at least eight characters wide, so I\u2019d like to adjust the column by 10 each time. Here\u2019s a basic loop that will:<\/p>\n<ul>\n<li>Set the start position<\/li>\n<li>Adjust the column by 10 each time<\/li>\n<li>Place a character from the array on the screen<\/li>\n<\/ul>\n<p style=\"padding-left: 90px\"><code># Clear the Screen\nClear-Host\n$StartPosition=$host.ui.RawUI.CursorPosition<\/code><\/p>\n<p style=\"padding-left: 90px\"><code># Set the Home Position for the clock\n$StartPosition.Y=3\n$StartPosition.X=4<\/code><\/p>\n<p style=\"padding-left: 90px\"><code># Remember where we started\n$CurrentPosition=$StartPosition\nFor ($Count=0; $Count -lt ($HexTime.Length); $Count++)\n{<\/code><\/p>\n<p style=\"padding-left: 90px\"><code># Set the cursor location\n$Host.Ui.RawUI.CursorPosition=$CurrentPosition<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>$Character=($HexArray[('0123456789abcdef\/-' |`\nSelect-String -pattern ($HexTime[$Count])).Matches.Index])\n$Character<\/code><\/p>\n<p style=\"padding-left: 90px\"><code># Bump up the counter for the Column and reset the row\n$CurrentPosition.X = $CurrentPosition.X+10\n$CurrentPosition.Y = $StartPosition.Y\n}<\/code><\/p>\n<p>But when we run through the loop, we get this result:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062316.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78985\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062316.png\" alt=\"Screenshot of results from running through the loop.\" width=\"568\" height=\"128\" \/><\/a><\/p>\n<p>My first row of each character is placed correctly, but the rest seem to end up on the same spot. Why is that?<\/p>\n<p>The trick is that with each <strong>Here-String<\/strong> is a character that forces the cursor back to column 0 for the additional rows.<\/p>\n<p>If only there was a way to place each row of the <strong>Here-String<\/strong> and set the cursor position. We\u2019ll go into that way tomorrow, of course!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use PowerShell to navigate the neutral zone. Honorary Scripting Guy, Sean Kearney, is here today to continue forth (not COBOL nor even Visual BASIC) into our silly yet fun quest to build a hexadecimal clock by using the PowerShell console. It does seem silly, doesn\u2019t it? Yet, consider some of the things that we\u2019ve [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,685,641],"tags":[56,154,45],"class_list":["post-78966","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use PowerShell to navigate the neutral zone. Honorary Scripting Guy, Sean Kearney, is here today to continue forth (not COBOL nor even Visual BASIC) into our silly yet fun quest to build a hexadecimal clock by using the PowerShell console. It does seem silly, doesn\u2019t it? Yet, consider some of the things that we\u2019ve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78966","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=78966"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78966\/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=78966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}