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

Discussion in 'Scripting' started by Mr K, Aug 4, 2005.

  1. Mr K

    Mr K Guest

    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?
     
    Mr K, Aug 4, 2005
    #1
    1. Advertisements

  2. Mr K

    Mr K Guest

    Hmm... I should specify that a VBScript example would be the most useful to me.
     
    Mr K, Aug 4, 2005
    #2
    1. Advertisements

  3. Mr K

    Marty List Guest

    Which scripting language are you using?
     
    Marty List, Aug 4, 2005
    #3
  4. Mr K

    Marty List Guest

    Marty List, Aug 4, 2005
    #4
  5. Mr K

    Mr K Guest

    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
     
    Mr K, Aug 4, 2005
    #5
  6. Mr K

    Marty List Guest

    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
     
    Marty List, Aug 4, 2005
    #6
  7. Mr K

    Mr K Guest

    I found the issue. It is the way I am asking for data. I should be using
    objGroup.GUID not objGroup.objectGUID.
     
    Mr K, Aug 4, 2005
    #7
  8. Mr K

    Wayne Tilton Guest

    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
     
    Wayne Tilton, Aug 4, 2005
    #8
  9. Mr K

    Mr K Guest

    hanks Wayne and Marty! My scripters block has been resolved.
     
    Mr K, Aug 5, 2005
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.