Avatar of yo_bee
yo_bee
Flag for United States of America asked on

Adding attribute values to <not set>

I am trying to setup a VB console app so I can schedule certain updateds to users Attributes.
I have created a simple script that queries all the user and if the CN matches the user it will update the altRecipient attribute from <not set> to a ADsPath.
I am able to clear the attribute, but not able to update.

I was also trying to add arg so I can run this without modifying the code, but this is the second part.  I need to get the first part to work. ;)

Imports System.Text.RegularExpressions
Imports System.DirectoryServices


Module Module1

    Sub Main()

        Const ADS_PROPERTY_CLEAR = 1
        Const ADS_PROPERTY_UPDATE = 2
        Const ADS_PROPERTY_APPEND = 3
        Const ADS_PROPERTY_DELETE = 4

 


        Dim objRootDSE
        Dim objSchemaContainer
        Dim strSchemaPath
        'Get the Root DSE from a random DC'
        objRootDSE = GetObject("LDAP://RootDSE")
        'Get the Schema NC path for the domain'
        strSchemaPath = objRootDSE.Get("defaultNamingContext")
        'Connect to the schema container on a random DC'
        objSchemaContainer = GetObject("LDAP://" & strSchemaPath)

        Dim objSearch As New DirectorySearcher()
        objSearch.SearchRoot = New DirectoryEntry(objSchemaContainer)
                objSearch.Filter = "(&(objectCategory=user))" 
        objSearch.SearchScope = SearchScope.Subtree

        objSearch.PropertiesToLoad.Add("cn")
        objSearch.PropertiesToLoad.Add("distinguishedName")
        objSearch.PropertiesToLoad.Add("ADsPath")
        Dim colQueryResults As SearchResultCollection
        colQueryResults = objSearch.FindAll()
        Dim objResult As SearchResult
        For Each objResult In colQueryResults
            If objResult.Properties("cn")(0) = "<username>" Then
                
                
                Console.WriteLine(objResult.Properties("cn")(0))
                Console.WriteLine(objResult.Properties("distinguishedName")(0))
                Console.WriteLine(objResult.Properties("ADsPath")(0))
                Dim ObjUser As Object
                Dim StrAtt As String
                StrAtt = objResult.Properties("ADsPath")(0)
                Console.WriteLine(StrAtt)
                ObjUser = GetObject(StrAtt)
                ObjUser.Putex(ADS_PROPERTY_CLEAR, "altRecipient", "CN=<username>,OU=IT Staff,DC=<domain>,DC=local"))

                ObjUser.SetInfo()
            End If

        Next

    End Sub

   

End Module

Open in new window

DatabasesActive DirectoryVB Script

Avatar of undefined
Last Comment
yo_bee

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
yo_bee

ASKER
That works if there is a value already, but of the attribute is  this method does not seem to work.
Chris Dent


There's no reason for it to fail. Does it return an error message?

Chris
yo_bee

ASKER
I do get an error, but at the present time I do not have it. When I am able to run this again I will post the error.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Chris Dent

That would be good, thanks :)

Chris
yo_bee

ASKER
I do get an error, but at the present time I do not have it. When I am able to run this again I will post the error.
yo_bee

ASKER
The Put option worked fine.
No Errors
Thanks
Could you help me with my second part of my questions?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent


Sure. Have you looked at System.Environment.GetCommandLineArgs?

http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx

The example on there is pretty good, it's not really any different from the example I'd give you. Does that go in the direction you need?

Chris
yo_bee

ASKER
Worked perfectly