convert ObjectGuid attribute to HEX

I need to convert a computers active directory GUID into hex. The format I need this in is displayed the way I need it when I view the GUID through Active Directory Users and Computers, but every attempt to convert the return from [adsi] returns a different result than I get when I view the value through the application. 

All I want to do is display the following in HEX

([adsi]"LDAP://CN=SomeComputer,CN=Computers,DC=SomeDC,DC=com").objectguid

July 29th, 2013 7:21pm

$guid = ([ADSI]'LDAP://CN=SomeComputer,CN=Computers,DC=SomeDC,DC=com').Properties['objectguid'].item(0)
$guidInHEX = [string]::Empty
$guid | % { $guidInHEX += '{0:X}' -f $_ }
$guidInHEX


  • Marked as answer by Oldguard Tuesday, July 30, 2013 4:29 PM
Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 7:45pm

If you would like the GUID to appear exactly as its hex equivalent appears in the Attribute Editor in ADUC, you can do the following:

$val = ([adsi]"LDAP://CN=SomeComputer,CN=Computers,DC=SomeDC,DC=com").ObjectGuid

[System.BitConverter]::ToString([byte[]]$val.Clone()).Replace('-',' ')

I'm using Clone here to get around the type casting.

July 29th, 2013 7:57pm

This is how it's displayed in Attribute editor:

$bytes =([adsi]"LDAP://CN=SomeComputer,CN=Computers,DC=SomeDC,DC=com").objectguid

$guid = new-object guid(,$bytes[0])

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 9:29pm

There must be some different rules on the GUID syntax produced by the different methods:

My method:

71ACE0F06EA47D4395D2ACC82F57F644

Neil's method:

71 AC E0 F0 6E A4 7D 43 95 D2 AC C8 2F 57 F6 44

Piotrek82's method:

Guid                                                                                                               
----                                                                                                               
f0e0ac71-a46e-437d-95d2-acc82f57f644

Notice the hex numbers are arranged differently using System.GUID.  Perhaps I'm missing something on the format of GUID.


July 29th, 2013 9:50pm

All three are correct.  Yours and mine are the same, I just added spaces between each byte as this is how it is displayed when you use the attribute editor in ADUC.  The System.Guid method is returning the same answer but the endianness is reversed (I believe System.Guid will always return Big endian but not 100% sure, I've only tried it on Little endian systems).

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 11:23pm

Yes I'm still confused about which one is the industry standard.
July 29th, 2013 11:48pm

If you're trying to convert in T-SQL it's quite simple: All you gotta do is query LDAP from SQL Server and cast the objectID as a uniqueidentifier.

"CAST(objectGUID AS uniqueidentifier)" and you will convert "0x71ACE0F06EA47D4395D2ACC82F57F644" to "f0e0ac71-a46e-437d-95d2-acc82f57f644" in no time; the way it's displayed in the Attribute Editor tab in ADSI Edit.

Cheers

Free Windows Admin Tool Kit Click here and download it now
January 31st, 2014 10:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics