Home All Groups Group Topic Archive Search About

Retrieving an object's GUID as a string from Active Directory



Author
4 Aug 2005 3:31 PM
Mr K
I'm in over my head. I found some code that takes the mystery out of getting
an object's SID and formatting it as a string but unfortunately that code is
not going to work with an object's GUID. Can anybody help me out with a
snipet to convert the object's GUID to a string value?

Author
4 Aug 2005 3:35 PM
Mr K
Hmm... I should specify that a VBScript example would be the most useful to me.

Show quote
"Mr K" wrote:

> I'm in over my head. I found some code that takes the mystery out of getting
> an object's SID and formatting it as a string but unfortunately that code is
> not going to work with an object's GUID. Can anybody help me out with a
> snipet to convert the object's GUID to a string value?
Author
4 Aug 2005 3:44 PM
Marty List
See if this gives you what you need (this URL should be one line in case it
wraps):
http://msdn.microsoft.com/library/en-us/ad/ad/reading_an_objectampaposs_objectguid_and_creating_a_string_representation_of_the_guid.asp


Show quote
"Mr K" <M**@discussions.microsoft.com> wrote in message
news:A8A6325B-7AFE-47CE-8C3E-11D6A223AE53@microsoft.com...
> Hmm... I should specify that a VBScript example would be the most useful to
me.
>
> "Mr K" wrote:
>
> > I'm in over my head. I found some code that takes the mystery out of getting
> > an object's SID and formatting it as a string but unfortunately that code is
> > not going to work with an object's GUID. Can anybody help me out with a
> > snipet to convert the object's GUID to a string value?
Author
4 Aug 2005 3:38 PM
Marty List
Which scripting language are you using?


Show quote
"Mr K" <Mr K@discussions.microsoft.com> wrote in message
news:1A044B0D-F033-436B-B1E0-E28B0395FABE@microsoft.com...
> I'm in over my head. I found some code that takes the mystery out of getting
> an object's SID and formatting it as a string but unfortunately that code is
> not going to work with an object's GUID. Can anybody help me out with a
> snipet to convert the object's GUID to a string value?
Author
4 Aug 2005 4:24 PM
Mr K
The page you supplied has a VB example but the problem is that things do not
seem so straight forward with VBScript.

If I run the following script the output is a bunch of question marks...

ADGroup = InputBox("I.E. F00-SQLSERVERENTMGR-2000", "Please enter Active
Directory group", "F00-SQLSERVERENTMGR-2000")

Set objGroup = GetObject("LDAP://CN=" & ADGroup & ",OU=Applications,OU=Users
and Groups,DC=f00bar,DC=f00")

WScript.Echo objGroup.objectGUID
Set GUID = objGroup.objectGUID
Author
4 Aug 2005 4:52 PM
Marty List
Your script is using the .objectGUID property, but the example I posted uses the
..GUID property. Also, your script is using "SET =" but that's not necessary
because "The IADs interface converts the GUID from an octet string, as stored on
a directory server, into a string format":
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/iads_property_methods.asp


This is the example script, slightly modified for VBScript.  It works for me,
using both global security groups and user accounts:


(Watch for line wraps)

Dim sADsPathObject
Dim sObjectGUID
Dim sBindByGuidStr

Dim IADsObject
Dim IADsObject2
On Error Resume Next

' Query the user for an ADsPath to start.
sADsPathObject = InputBox("This code binds to a directory object by ADsPath,
retrieves the GUID, then rebinds by GUID." & vbCrLf & vbCrLf & "Specify the
ADsPath of the object to bind to :")

If sADsPathObject = "" Then
    WScript.Quit
End If

MsgBox "Binding to " & sADsPathObject

' Bind to initial object.
Set IADsObject = GetObject(sADsPathObject)

If (Err.Number <> 0) Then
   MsgBox Err.Number & " on GetObject method"
   WScript.Quit
End If

' Save the GUID of the object.
sObjectGUID = IADsObject.Guid

MsgBox "The GUID for " & vbCrLf & vbCrLf & sADsPathObject & vbCrLf & vbCrLf & "
is " & vbCrLf & vbCrLf & sObjectGUID

' Release the initial object.
Set IADsObject = Nothing

' Build a string for Binding to the object by GUID.
sBindByGuidStr = "LDAP://<GUID=" & sObjectGUID & ">"

MsgBox sBindByGuidStr

' Bind BACK to the Same object using the GUID.
Set IADsObject = GetObject(sBindByGuidStr)

If (Err.Number <> 0) Then
   MsgBox Err.Number & " on GetObject method "
   WScript.Quit
End If

MsgBox "Successfully RE bound to " & sADsPathObject & vbCrLf & vbCrLf & " using
the path:" & vbCrLf & vbCrLf & sBindByGuidStr




Show quote
"Mr K" <M**@discussions.microsoft.com> wrote in message
news:A37F76B2-7199-4EE7-8828-201CB5D2286E@microsoft.com...
> The page you supplied has a VB example but the problem is that things do not
> seem so straight forward with VBScript.
>
> If I run the following script the output is a bunch of question marks...
>
> ADGroup = InputBox("I.E. F00-SQLSERVERENTMGR-2000", "Please enter Active
> Directory group", "F00-SQLSERVERENTMGR-2000")
>
> Set objGroup = GetObject("LDAP://CN=" & ADGroup & ",OU=Applications,OU=Users
> and Groups,DC=f00bar,DC=f00")
>
> WScript.Echo objGroup.objectGUID
> Set GUID = objGroup.objectGUID
Author
4 Aug 2005 4:55 PM
Mr K
I found the issue. It is the way I am asking for data. I should be using
objGroup.GUID not objGroup.objectGUID.

Show quote
"Mr K" wrote:

> The page you supplied has a VB example but the problem is that things do not
> seem so straight forward with VBScript.
>
> If I run the following script the output is a bunch of question marks...
>
> ADGroup = InputBox("I.E. F00-SQLSERVERENTMGR-2000", "Please enter Active
> Directory group", "F00-SQLSERVERENTMGR-2000")
>
> Set objGroup = GetObject("LDAP://CN=" & ADGroup & ",OU=Applications,OU=Users
> and Groups,DC=f00bar,DC=f00")
>
> WScript.Echo objGroup.objectGUID
> Set GUID = objGroup.objectGUID
Author
4 Aug 2005 4:58 PM
Wayne Tilton
Show quote
"=?Utf-8?B?TXIgSw==?=" <M**@discussions.microsoft.com> wrote in
news:A37F76B2-7199-4EE7-8828-201CB5D2286E@microsoft.com:

> The page you supplied has a VB example but the problem is that things
> do not seem so straight forward with VBScript.
>
> If I run the following script the output is a bunch of question
> marks...
>
> ADGroup = InputBox("I.E. F00-SQLSERVERENTMGR-2000", "Please enter
> Active Directory group", "F00-SQLSERVERENTMGR-2000")
>
> Set objGroup = GetObject("LDAP://CN=" & ADGroup &
> ",OU=Applications,OU=Users and Groups,DC=f00bar,DC=f00")
>
> WScript.Echo objGroup.objectGUID
> Set GUID = objGroup.objectGUID
>

This function takes an AdsPath (i.e. full LDAP:// or WinNT:// path of the
object) and returns the GUID is readable format.  Watch for line
wrapping.

HTH,

Wayne

Function GetGUID(theObject)
   GUID = GetObject(theObject).Get("objectGUID")
   strGUID = ""
   For x = LBound(GUID) to UBound(GUID)
      strGUID = strGUID & Right("00" & Hex(AscB(MidB(GUID, x+1))),2)
   Next
   vvvvvvvv = BigEndian(Mid(strGUID,1,8))
   wwww = BigEndian(Mid(strGUID,9,4))
   xxxx = BigEndian(Mid(strGUID,13,4))
   yyyy = Mid(strGUID,17,4)
   zzzzzzzzzzz = Mid(strGUID,21)
   GetGUID = "{" & lcase(vvvvvvvv & "-" & wwww & "-" & xxxx & "-" & yyyy
& "-" & zzzzzzzzzzz) & "}"
End Function

' Flips the bytes in a string of bytes from little endian to big endian
format
' and converts to decimal
Function BigEndian(theBytes)
   BigEndian = ""
   For x = Len(theBytes) To 2 step -2
      BigEndian = BigEndian & Mid(theBytes,x-1,2)
   Next
End Function
Author
5 Aug 2005 3:57 PM
Mr K
hanks Wayne and Marty! My scripters block has been resolved.

AddThis Social Bookmark Button