{"id":79005,"date":"2016-06-24T00:01:02","date_gmt":"2016-06-24T07:01:02","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=79005"},"modified":"2019-02-18T09:10:35","modified_gmt":"2019-02-18T16:10:35","slug":"build-a-hexadecimal-clock-in-powershell-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-a-hexadecimal-clock-in-powershell-part-5\/","title":{"rendered":"Build a hexadecimal clock in PowerShell \u2013 Part 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn to split data in a Here-String and get a console beep.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here with our final day this week to wrap up building a hexadecimal clock in PowerShell.<\/p>\n<p>When we finished yesterday, we ran across a snag in our output. Most of our rows kept dropping to the immediate left of the screen as in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-79015\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062416.png\" alt=\"Screenshot of badly formatted rows.\" width=\"568\" height=\"128\" \/><\/a><\/p>\n<p>For this, we will look at one of our <strong>Here-String<\/strong>s to try and figure out the problem:<\/p>\n<p style=\"padding-left: 90px\"><code>$HexArray[8]=@'<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>\u00a0888888<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>88\u00a0\u00a0\u00a0 88<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>88\u00a0\u00a0\u00a0 88<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>\u00a0888888<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>88\u00a0\u00a0\u00a0 88<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>88\u00a0\u00a0\u00a0 88<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>\u00a0888888<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>'@<\/code><\/p>\n<p>If we look at a <strong>Here-String<\/strong>, you\u2019ll notice it is <u>literally<\/u> a long string. It has an array count of 1 but a large length.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-79025\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062416.png\" alt=\"Screenshot that shows the count and length values of the array.\" width=\"451\" height=\"175\" \/><\/a><\/p>\n<p>The question is, how can I access the individual rows? If we can identify the character at the end of a line, we could feed that to a <strong>split()<\/strong> method. This will turn this <strong>Here-String<\/strong> temporarily into an array.<\/p>\n<p>We can run through a <strong>Here-String<\/strong> with this small loop in PowerShell to examine and view its values. The <strong>[byte][char]<\/strong> converts the letter or character to the ASCII number.<\/p>\n<p style=\"padding-left: 90px\"><code>for ($x=0; $x -lt $HexArray[8].Length; $x++)\n{<\/code><\/p>\n<p style=\"padding-left: 120px\"><code>\n$Char=$HexArray[8][$x]\nWrite-Host $Char,([byte][char]$Char)\n<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>}<\/code><\/p>\n<p>The output will look something like this, and please note the repeating patterns of 13 and 10 on the screen.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-062416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-79026\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-062416.png\" alt=\"Screenshot that shows ASCII values for characters at the end of lines.\" width=\"99\" height=\"638\" \/><\/a><\/p>\n<p>We can send error Character 13 (Carriage Return) or Character 10 (Linefeed) in to split this into an array. We\u2019ll use the line, Linefeed, as our split character.<\/p>\n<p style=\"padding-left: 90px\"><code>$CharacterArray=$HexArray[8].split([char][byte]10)<\/code><\/p>\n<p>Now if we examine this, we have an array of data that we can step through.<\/p>\n<p style=\"padding-left: 90px\"><code>$CharacterArray.Count<\/code><\/p>\n<p>Now that the data is split into an array, we can step through this row-by-row and set the column each time. Here is our original loop that\u2019s adjusted to split the <strong>Here-String<\/strong> and <u>then<\/u> output the data.<\/p>\n<p style=\"padding-left: 90px\"><code>For ($Count=0; $Count -lt ($HexTime.Length); $Count++)\n{<\/code><\/p>\n<p style=\"padding-left: 120px\"><code>$Character=($HexArray[('0123456789abcdef\/-' |`\nSelect-String -pattern ($HexTime[$Count])).Matches.Index])\n$CharacterArray=$Character.split([char][byte]10)<\/code><\/p>\n<p style=\"padding-left: 120px\"><code># Loop through all members of the particular Here-String\nFor ($Row=0; $Row -lt $CharacterArray.count;$Row++)\n{<\/code><\/p>\n<p style=\"padding-left: 150px\"><code>\n# Position the Cursor\n$Host.UI.Rawui.CursorPosition=$CurrentPosition<\/code><\/p>\n<p style=\"padding-left: 150px\"><code># Access the Value and print it\n$CharacterArray[$Row]<\/code><\/p>\n<p style=\"padding-left: 150px\"><code># Move up the cursor to the next row\n$CurrentPosition.Y=$CurrentPosition.Y+1<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>\u00a0 \u00a0 }<\/code><\/p>\n<p style=\"padding-left: 120px\"><code>\n# Bump up the counter for the Column and reset the row\n$CurrentPosition.X = $CurrentPosition.X+10\n$CurrentPosition.Y = $StartPosition.Y<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>\n}<\/code><\/p>\n<p>As it stands, we just drew a single row on the screen. To make this continue, we\u2019ll need to wrap this whole operation in a <strong>do<\/strong> loop. One problem with this approach is that there will be a <u>lot<\/u> of flickering on the screen. What we <u>really<\/u> want is to update the clock each second that it changes.<\/p>\n<p>For this, we\u2019ll add in a simple trap for the time. We\u2019ll remember the last time that it changed.<\/p>\n<p style=\"padding-left: 90px\"><code>do\n{<\/code><\/p>\n<p style=\"padding-left: 120px\"><code>\n$HexTime=Get-HexTime<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>\n} Until ($Hextime -ne $LastHexTime)<\/code><\/p>\n<p>Then, at the end of the loop, we\u2019ll drop in a simple line like this:<\/p>\n<p style=\"padding-left: 90px\"><code>$LastHexTime = $Hextime<\/code><\/p>\n<p>One more fun thing. Wouldn\u2019t it be cool to have a little \u201cTic\u201d sound as each second passes by? (Well, I thought it would, or at least a neat way to irritate my coworkers \ud83d\ude09 )<\/p>\n<p>We can do this with a simple <u>beep<\/u> on the console. I tweaked it down to a <u>very<\/u> short time and actually got a \u201cTic\u201d sound.<\/p>\n<p style=\"padding-left: 90px\"><code>[console]::beep(1000,5)<\/code><\/p>\n<p>Wonder what it all looks like? You can download this script from the <a href=\"https:\/\/gallery.technet.microsoft.com\/Windows-PowerShell-23ed0697\" target=\"_blank\">TechNet Script Repository<\/a>.<\/p>\n<p>That\u2019s all there is to turning your PowerShell console into a hexadecimal clock! Thanks for sharing the time with us!<\/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\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn to split data in a Here-String and get a console beep. Honorary Scripting Guy, Sean Kearney, is here with our final day this week to wrap up building a hexadecimal clock in PowerShell. When we finished yesterday, we ran across a snag in our output. Most of our rows kept dropping to the [&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-79005","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: Learn to split data in a Here-String and get a console beep. Honorary Scripting Guy, Sean Kearney, is here with our final day this week to wrap up building a hexadecimal clock in PowerShell. When we finished yesterday, we ran across a snag in our output. Most of our rows kept dropping to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/79005","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=79005"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/79005\/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=79005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=79005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=79005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}