Converting a GUID string to octet string

GUID’s seem to pop up everywhere these days. Where ever there’s a need to uniquely identify an object there’s a good chance that the GUID fits the bill.

The two main formats that I’ve encountered them in in Active Directory and Windows Installer has been string, or binary octet string format. Now despite the fact they look quite different, they are in fact the same.

Here’s the process:
1) Strip out the hyphens

Looking at the remaining string in bytes (groupings of pairs) then
2) Reverse the order in the first 3 bytes
3) concatenate the remaining bytes with the reversed bytes.

for example:
7147a8de-129a-4edd-9533-83982050211f
becomes:
dea847719a12dd4e953383982050211f

And our sample code:

# Convert GUID to Octet String
# Each pair needs to be written in Hex in the format: 4 bytes-2 bytes-2 bytes-2 bytes-6 bytes
# The first 3 byte sequences are written in reverse.
# —————————————————————————————————
function ReverseBytes
# —————————————————————————————————
{
Param (
$StringOfBytes
)
for ($i=0; $i -lt $StringOfBytes.length; $i+=2)
{
$NuString= $StringOfBytes.substring($i,2)+$NuString
}
return $NuString
}
# —————————————————————————————————
function convertguid
# —————————————————————————————————
{
Param (
$guid
)
$partA, $PartB, $PartC, $TheRest = $guid.split(“-”)
return ( ReverseBytes $PartA ) + ( ReverseBytes $PartB ) `
+ ( ReverseBytes $PartC ) + $TheRest
}
# —————————————————————————————————
$a = [system.guid]::NewGuid().ToString()
write-host $a
convertguid ( $a )

Power Lines

Once you’ve been using PowerShell for a while you start to notice that you can often get the job done using just one line of code. These “one liners” can sometimes look pretty impressive, but they are also get pretty cryptic!

Its a good sales pitch for PowerShell: “yeah, I can do [insert amazing feat here]. It just takes a line of code in PowerShell!” ;)

I like to think of these, as small bragging rights for the new language, and I call them Power Lines. As I find interesting Power Lines during my wanderings I will start posting them here.

Of course the most impressive Power Lines I’ve seen around originate from developers, so I make no promises on the “amazement” factor ;)

Starting off small here’s the first one that I’ve been using today:

[system.guid]::NewGuid().ToString()

This simply generates a new GUID in string format.

A couple of other examples I’ve seen around:
MS PowerShell One Liners
MOW’s old blog
Google

What’s your Power Line?

Static Members and PowerShell obscurity

I recently posted a question on the Microsoft PowerShell NG on how to use a .Net function in PowerShell.

It might be easier if I put this in context. I had a requirement to take a string containing a mixture of upper and lowercase letters, and rewrite it with a hyphen preceding the uppercase letters. This didn’t seem like a particularly daunting task. As MSDN is my new best friend I thought I’d have a browse and see what functions may be able to help.

I found the IsUpper function and then went about trying to use Get-Member to try and see if I could expose this within PowerShell. No dice.

Duncan Smith was kind enough to provide some light on the subject.

So within PowerShell we can access this as a Static Member. Now I’ve seen a few static members used in PowerShell scripts so far, but as a non-coder being able to uncover these to access functions and methods within .Net still seems something of a black art to me. I’m hoping that as time goes by, and my exposure to more and more programming concepts these things will start to become more apparent. I’ll let you know ;)

And below is my script to give you another example:

$MyString = “ThisIsMyString”
$NuString = “”

for ($i=0; $i -lt $MyString.length; $i++)
{
If ([char]::IsUpper($MyString.chars($i)))
{
$NuString = $NuString+”-”+$MyString.Chars($i)
}
Else
{
$NuString = $NuString+$MyString.Chars($i)
}
}
# trim the leading “-” if the string starts with a capital.
$NuString = $NuString.substring(1,$NuString.length-1)

write-host $NuString