Announcement

Collapse
No announcement yet.

List all GUIDs in AD

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • List all GUIDs in AD

    Hi all,

    I need to somehow export into preferbly excel (csv) a list of all of our users in AD with Username Display Name and most importantly GUID.Any body any ideas?

  • #2
    Re: List all GUIDs in AD

    You can do that with a vbscript.

    The the sample below, the columns in the output successively are:
    User Name / active / Display Name / GUID string / GUID-hexadecimal

    Where "user name" is the windowsNT accountname of the user.
    In most programs the GUID is showed in string format with separators and wrapped between curly brackets. However the GUID is stored in a Hexadecimal form. When binding to an object by using the GUID you'll need the Hexadecimal GUID form, that is why I put that one also in the csv-file.

    sample
    Code:
    Set objRootDSE = GetObject("LDAP://rootDSE")
    strDNDomain = objRootDSE.Get("defaultNamingContext")
    
    
    ':: variables for query
    StartNode = strDNDomain
    SearchScope = "subtree"
    
    FilterString = "(&(objectCategory=person)(objectClass=user))"
    Attributes = "adspath"
    
    ':: Output csv-file
    outFile = "c:\test.CSV"
    
    '-----------------------------------------------------------------------
    ':: Start the ADO connection
    Set objCommand = CreateObject("ADODB.Command")
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    objCommand.ActiveConnection = objConnection
    
    ':: Create the LDAP-Query
    LDAPQuery = "<LDAP://" & StartNode & ">;" & FilterString & ";" _
            & Attributes & ";" & SearchScope
    
    objCommand.CommandText = LDAPQuery
    objCommand.Properties("Page Size") = 100
    objCommand.Properties("Timeout") = 30
    objCommand.Properties("Cache Results") = False
    
    ':: create the recordsetobject
    Set objRecordSet = objCommand.Execute
    '-----------------------------------------------------------------------
    
    ':: Create new csv-file for writing
    ' http://msdn.microsoft.com/en-us/library/czxefwt8(VS.85).aspx
    With CreateObject("scripting.filesystemobject")
       Set objFile = .CreateTextFile(outFile, True)
       comma =  ""","""
       objFile.WriteLine "User Name,active,Display Name,GUID string,GUID-hexadecimal"
    End With
    
    ':: loop through the records from the recordset
    On Error resume Next
    If not objRecordSet.eof then 
       objRecordSet.MoveFirst
    
       Do Until objRecordSet.EOF
          Disabled = Empty
          strADSPath = objRecordSet.Fields("ADsPath").Value
    
          Set objUser = GetObject(strADSPath)
          hexGUID = objUser.GUID
          strGUID = HexToStrGUID(hexGUID)
          If objUser.AccountDisabled = TRUE then Disabled = "X"
    
          nwLine = """" & objUser.sAMAccountname & comma _
                        & Disabled & comma _
                        & objUser.displayName & comma _
                        & strGUID & comma & hexGUID & """"
          objFile.WriteLine nwLine
    
          rem  'checkpoint, use the GUID to bind to the object
          rem  Set objFromGUID = GetObject("LDAP://<GUID=" & hexGUID & ">")
          rem  WScript.Echo objFromGUID.Get("distinguishedName")
    
          objRecordSet.MoveNext
       Loop
    End If
    objFile.close : Set objFile = Nothing
    objRecordset.Close : objConnection.Close
    
    wscript.echo "Done"
    
    
    Function HexToStrGUID(ByVal strHex)
       '===============================================
       ' http://support.microsoft.com/kb/325649
       ' convert the hexadecimal string (base 16) form 
       ' of an object's GUID into its string form.
       '===============================================
       Dim GUIDStr
    
       'Convert the string by flipping the bits around.
       GUIDStr = Mid (strHex, 7, 2)
       GUIDStr = GUIDStr + Mid (strHex,  5, 2)
       GUIDStr = GUIDStr + Mid (strHex,  3, 2)
       GUIDStr = GUIDStr + Mid (strHex,  1, 2)
       GUIDStr = GUIDStr + Mid (strHex, 11, 2)
       GUIDStr = GUIDStr + Mid (strHex,  9, 2)
       GUIDStr = GUIDStr + Mid (strHex, 15, 2)
       GUIDStr = GUIDStr + Mid (strHex, 13, 2)
       GUIDStr = GUIDStr + Mid (strHex, 17, Len(strHex))
    
       HexToStrGUID = "{" & Mid (GUIDStr,  1,  8) & _
                      "-" & Mid (GUIDStr,  9,  4) & _
                      "-" & Mid (GUIDStr, 13,  4) & _
                      "-" & Mid (GUIDStr, 17,  4) & _
                      "-" & Mid (GUIDStr, 21, 15) & "}"
    End Function
    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: List all GUIDs in AD

      Thanks Rems!! that is exactly what i was after and worked a treat!! you're a life saver!!

      Comment

      Working...
      X