troubleshooting Question

Delete an AD Attribute via VBScript

Avatar of JAaron Anderson
JAaron AndersonFlag for United States of America asked on
ProgrammingActive DirectoryVB Script
46 Comments2 Solutions2971 ViewsLast Modified:
Since the Google Link is no longer available...
Im trying to perform this in VBscript.

I have 2 attributes I want to not just make null but delete them expongued from the User record.


please help thx

I ref'd. https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Windows/2000/Q_21192917.html#a36969166 but the link doesnt exist any more hoping to keep example code inline on post for benefit of all users...


also I have something like this to hunt for Group membership but not sure what the objType equivalent is to "group" for retrieving all properties of a person. is it "user" ?


thanks
            
... my local variables declared above ...

strUserToPurge = InputBox("Type in the ID of user", "PURGE USER")
If InStr(strUserToPurge, "\") > 0 Then strUserToPurge = Mid(strUserToPurge, InStr(strUserToPurge, "\") + 1)

strUserPath = Get_LDAP_User_Properties("user", "name", strMyDomain & "\" & strUserToPurge, "adspath", strConnUser, strConnPassword)
' I have something like this to hunt for Group membership but not sure what the objType equivalent is to "group" for retrieving all properties of a person. is it "user" ?

' Delete target Attribute 	strAttribute1 = "myattribute1"
	Set objUser = GetObject(objRecordSet.Fields("strUserPath").Value)
	      objUser.PutEx ADS_PROPERTY_CLEAR, "myattribute1", 0 ' Completely erase 
	objUser.SetInfo


' Delete target Attribute 	strAttribute2 = "myattribute2"
	Set objUser = GetObject(objRecordSet.Fields("strUserPath").Value)
	      objUser.PutEx ADS_PROPERTY_CLEAR, "myattribute2", 0 ' Completely erase 
	objUser.SetInfo


WScript.Quit


' *************************************************
' * Functions
' *************************************************

Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps, strConUser, strConPwd)
      

      If InStr(strObjectToGet, "\") > 0 Then
            arrGroupBits = Split(strObjectToGet, "\")
            strDC = arrGroupBits(0)
            strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
            strObjectToGet = arrGroupBits(1)
      Else
      ' Otherwise we just connect to the default domain
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDNSDomain = objRootDSE.Get("defaultNamingContext")
      End If
 
      Const ADS_SERVER_BIND = &H200
      Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	
      strBase = "<LDAP://" & strDNSDomain & ">"
      ' Setup ADO objects.
      Set adoCommand = CreateObject("ADODB.Command")
      Set ADOConnection = CreateObject("ADODB.Connection")
      ADOConnection.Provider = "ADsDSOObject"
      If IsNull(strConUser) = False Then ADOConnection.Properties("User ID") = strConUser
      If IsNull(strConPwd) = False Then ADOConnection.Properties("Password") = strConPwd
      If strDC <> "" Then ADOConnection.Properties("ADSI Flag") = ADS_SERVER_BIND
      ADOConnection.Open "Active Directory Provider"
      adoCommand.ActiveConnection = ADOConnection
 
 
      ' Filter on user objects.
      'strFilter = "(&(objectCategory=person)(objectClass=user))"
      strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"
 
      ' Comma delimited list of attribute values to retrieve.
      strAttributes = strCommaDelimProps
      arrProperties = Split(strCommaDelimProps, ",")
 
      ' Construct the LDAP syntax query.
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      ' Define the maximum records to return
      adoCommand.Properties("Page Size") = 100
      adoCommand.Properties("Timeout") = 30
      adoCommand.Properties("Cache Results") = False
      If strDC <> "" Then adoCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
 
      ' Run the query.
      Set adoRecordset = adoCommand.Execute
      ' Enumerate the resulting recordset.
      strReturnVal = ""
      Do Until adoRecordset.EOF
          ' Retrieve values and display.
          For intCount = LBound(arrProperties) To UBound(arrProperties)
                If strReturnVal = "" Then
                      strReturnVal = adoRecordset.Fields(intCount).Value
                Else
                      strReturnVal = strReturnVal & vbCrLf & adoRecordset.Fields(intCount).Value
                End If
          Next
          ' Move to the next record in the recordset.
          adoRecordset.MoveNext
      Loop
 
      ' Clean up.
      adoRecordset.Close
      ADOConnection.Close
      Get_LDAP_User_Properties = strReturnVal
 
End Function
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 46 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 46 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros