Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert a String to GUID

705 views
Skip to first unread message

wilt...@gmail.com

unread,
Jul 7, 2005, 2:38:34 PM7/7/05
to
In the process of install automation, we're trying to convert the
string representation of a GUID (EEF00083-3454-5c6c-9ACB-FC9E8394FC8D,
for example) to that of the actual GUID datatype appropriate to be
stored inside of Active Directory.

Is there an easy to to accomplish this?

Rob

Richard Mueller [MVP]

unread,
Jul 9, 2005, 9:06:42 AM7/9/05
to
Rob wrote:

Hi,

GUID's are stored in OctetString syntax, which is a byte array. Byte arrays
cannot be created or modified in VBScript. This could be done in VB, where
you can declare a varible as a byte array, but it would be a lot of work.
Your string would have to first be converted from what I call the display
form, to a hex string form. Then the hex string would be converted to a byte
array. You might be able to find API functions to assist you in VB.

The opposite conversion is possible in VBScript. The byte array can be
converted to a hex string, then the hex string converted to the display form
you used. Below is an example:

Set objUser = GetObject("LDAP://cn=JSmith,ou=Sales,dc=MyDomain,dc=com")

arrbytGuid = objUser.objectGuid
strHexGuid = OctetToHexStr(arrbytGuid)
Wscript.Echo "User Guid in hex string format: " & strHexGuid

strGuid = HexGuidToGuidStr(strHexGuid)
Wscript.Echo "User Guid in display format: " & strGuid

Function OctetToHexStr(arrbytOctet)
' Function to convert OctetString (byte array) to Hex string.

Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
OctetToHexStr = OctetToHexStr _
& Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
Next
End Function

Function HexGuidToGuidStr(strGuid)
' Function to convert Hex Guid to display form.
Dim k

HexGuidToGuidStr = ""
For k = 1 To 4
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 9 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 13 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 17 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 17, 4)
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 21)
End Function

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site - http://www.rlmueller.net
--


wilt...@gmail.com

unread,
Jul 11, 2005, 8:11:07 AM7/11/05
to
Richard,

Someone found this article on Microsoft that converts a string to an
OctetString GUID for querying AD...
http://support.microsoft.com/default.aspx?scid=kb;en-us;325648

My only issue now is trying to set the computer GUID at the time I'm
trying to create the object... For example:

Set oComputer = computerContainer.Create("computer", "CN=" &
sComputerName)
oComputer.Put "samAccountName", sComputerName + "$"
oComputer.Put "userAccountControl", lFlag
oComputer.Put = "objectGUID", ConvertStringGUIDToHexStringGUID(strGUID)
oComputer.SetInfo

Apparently, the objectGUID object doesn't support the Put method --
even at the time of creation. Any suggestions?

Rob

Richard Mueller [MVP]

unread,
Jul 11, 2005, 12:03:39 PM7/11/05
to
Hi,

The value of objectGuid is set by the system and cannot be modified.

http://msdn.microsoft.com/library/en-us/adschema/adschema/a_objectguid.asp

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site - http://www.rlmueller.net
--

<wilt...@gmail.com> wrote in message
news:1121083867.0...@z14g2000cwz.googlegroups.com...

Richard Mueller [MVP]

unread,
Jul 11, 2005, 12:41:44 PM7/11/05
to
Hi,

First, the code in the kb article you linked has a small error. The line:

ConvertGUIDToOctet = octetStr

should be:

ConvertStringGUIDToHexStringGUID = octetStr

Second, the function ConvertStringGUIDToHexStringGUID returns a string, not
a byte array. That's the key. Even if AD allowed modification of the
objectGUID attribute, you would need to create a byte array - TypeName
Byte().

Finally, I mis-spoke a bit earlier. Michael Harris pointed out a way to
create byte arrays in VBScript using the ADODB.Stream object and a temporary
file in this thread:

http://groups-beta.google.com/group/microsoft.public.scripting.vbscript/browse_thread/thread/d4e85508a5c775c8/f3ac2e1771527651?

Still, AD does not allow you to update objectGUID.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site - http://www.rlmueller.net
--

<wilt...@gmail.com> wrote in message
news:1121083867.0...@z14g2000cwz.googlegroups.com...

wilt...@gmail.com

unread,
Jul 11, 2005, 1:53:26 PM7/11/05
to
Just for clarification, I don't want to update the value -- I want to
set it when I create the computer object inside of the Computers
container -- it this at least possible?

If so, I can look into writing an ActiveX DLL in VB6 to call within
VBScript, passing it the string GUID for conversion?

wilt...@gmail.com

unread,
Jul 11, 2005, 2:00:55 PM7/11/05
to
Just took a second read of your post, Richard...

With regards with the objectGUID, if the 'system' sets it during
creation, then isn't there some way for the user to duplicate this
operation when they're using script to create the computer object
inside of AD?

For a general overview, we have a mix of MS and Linux systems... our
techs carry around a USB keychain with a specially compiled kernel that
pulls the GUID (in string format) of a system, along with the MAC
address of the NIC and sends that to a database, along with the
machine's host name.

A Cron job is run that pulls that information and enters the MAC
address and hostname into our DHCP table and we were hoping to use
VBScript to access the database, pull the string GUID, convert it and
create the computer object in AD to prepare it for a RIS install.

Richard Mueller [MVP]

unread,
Jul 11, 2005, 2:00:01 PM7/11/05
to
The values of several attributes are set by the system when the object is
created, including objectGUID. You cannot set your own value.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site - http://www.rlmueller.net
--
<wilt...@gmail.com> wrote in message

news:1121104406....@f14g2000cwb.googlegroups.com...

wilt...@gmail.com

unread,
Jul 12, 2005, 8:02:32 AM7/12/05
to
For folks who read this thread in the future and are attempting to do
we're trying to accomplish (pre-staging RIS installs):

http://support.microsoft.com/kb/302467

I just had the wrong object.

Rob

Rals

unread,
Aug 4, 2009, 10:50:38 AM8/4/09
to

Hi guys,

I am actually new to VB Script. I have this code below. While converting the
arrOctet (Byte Array) to a HexString this code ignores the termination
character and converts it (termination character) to its Hex equivalent. As
this array can be of variable lenght (which is decided at runtime) it adds
garbage values at the end of the HexString.

Is there a way to determine the termination character in the byte array and
convert only the valid characters ignoring the rest.

Dim k
OToHStr = ""
For k = 1 To To LenB(arrOctet)
OToHStr = OToHStr & Right("0" & Hex(AscB(MidB(arrbytOctet, k, 1))), 2)
Next

Thanks for all your help.

Rals.

url:http://www.ureader.com/msg/1677383.aspx

Richard Mueller [MVP]

unread,
Aug 4, 2009, 11:29:03 AM8/4/09
to

"Rals" <ral...@gmail.com> wrote in message news:...
> Message-ID: <5e20dac054194ff9...@newspe.com>
> X-Mailer: http://www.umailcampaign.com, ip log:213.123.200.131
> Newsgroups: microsoft.public.scripting.wsh
> NNTP-Posting-Host: 22.bb.5446.static.theplanet.com 70.84.187.34
> Path: TK2MSFTNGP01.phx.gbl!TK2MSFTNGP02.phx.gbl!newspe.com
> Lines: 1
> Xref: TK2MSFTNGP01.phx.gbl microsoft.public.scripting.wsh:86499

Your snippet is identical to code I've used for years, but the only byte
arrays I encounter are in Active Directory. I've never seen a termination
character in the array. You would need to test for the character in the
loop. For example, if the character is hex &h00 (ascii 000):
========
Dim k, strByte


OToHStr = ""
For k = 1 To To LenB(arrOctet)

strByte = Right("0" & Hex(AscB(MidB(arrbytOctet, k, 1))), 2)
If (strByte = "00") Then
Exit For
End If
OtoHStr = OToHStr & strByte
Next
========
This assumes the termination character is just that, it delimits the end and
all characters after that are ignored. You could also code to just skip the
character and process the rest.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


0 new messages