Summary: Use Select-String in PowerShell to identify data and match it to an array.
Honorary Scripting Guy, Sean Kearney, is here today to play more with our funky, nerdy, cool hexadecimal clock.
Yesterday we built out a small function named Get-HexTime to give us the current time that displays hex digits instead of decimal for hours, minutes, and seconds.
One of our challenges is going to be matching up the individual hex digits to our positions in the array. We can step through the list of characters in HexTime with a pretty simple loop:
$Hextime=Get-Hextime
For ($Count=0; $Count -lt ($HexTime.Length); $Count++)
{
$Character=$HexTime[$count]
}
With this loop in place, I just need to figure out what the characters were and maybe convert them to a numeric value to access the various elements of the array.
At first, I thought to myself, I’ll just find out what the ASCII character is by using the following:
[byte][char]’0’
[byte][char]’a’
This process reveals the ASCII code of the character. The numeric characters would be 0 to 9 (and in sequence) with values of 48 to 57.
The letters, a – f, would be 65 to 71. Of course, additional characters would have to be trapped for. My initial thoughts were that I could use the PowerShell switch statement and have it return the matching value in the following manner:
switch ($Character=$HexTime[$count])
{
'0' { $HexValue=0 }
'1' { $HexValue=1 }
...
'a' { $HexValue=10 }
...
'f' { $HexValue=15 }
}
You get the idea. But, first off, I hate to type repetitive information. Then I thought on a different line. I could take the sequence and pipe it into Select-String to find a match.
In the following example, I can have Select-String look for the ‘e’ and return an object that contains the exact position it was found in.
‘0123456789abcdef’ | Select-String -Pattern 'e'
Although the result seems to only be the same original string, if you pipe this to Get-Member, you’ll see some different properties. More importantly, you see a property named “Matches”.
If you access this property directly, you’ll see something really cool, a deeper property named Index, which is the exact position in the string where this value was found.
Because each character in our string is unique in both value and position, we can use this to match against our array.
Now here is how we could pull out the location of the value in the array:
# Get the Character from the String
$Character=$Hextime[$Count]
# Find what position it exists in
$Match=('0123456789abcdef/-' | Select-String –pattern $Character)
# Get that exact value
$Index=$Match.Matches.Index
To make it simpler, we could wrap it all up in one line like this:
$Index=('0123456789abcdef/-' | Select-String –pattern ($Hextime[$Count])).matches.index
Now that we’ve gotten all the hard work out of the way, tomorrow we’ll really have fun as we start to draw this output onto the screen!
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.
Until then always remember that with Great PowerShell comes Great Responsibility.
Sean Kearney
Honorary Scripting Guy
Cloud and Datacenter Management MVP