Manipulating Objects
Modifying objects with System.DirectoryServices
can be accomplished a couple of different ways. To modify an attribute that currently has a value, you can set it using the Properties
property. For example, the following code would modify the givenName
attribute:
objADObject.Properties("givenName")(0) = "Robert"
If you want to set an attribute that was previously unset, you must use the Properties.Add
method. The following code would set the previously unset sn
attribute:
objADObject.Properties("sn").Add("Robert")
To determine whether an attribute has been set, you can use Properties("
attribute-name
").Count
, which will return the number of values that have been set for the attribute. Just like with ADSI, all modifications are made initially to the local property cache and must be committed to the server. With ADSI, you would use the IADs::SetInfo( )
method, and with System.DirectoryServices
, it is called CommitChanges( )
, which is available from the DirectoryEntry
class.
objADObject.CommitChanges( )
Now that we covered how to set an attribute, we can modify the earlier code that printed all the values of an attribute to set an attribute instead. The code in Example 31-2 expects three command-line parameters: the first is the ADsPath
of the object to modify, the second is the attribute name, and the third is the value to set the attribute to.
Example 31-2. Setting an attribute
Dim strADsPath As String Dim strAttrName As String Dim strAttrValue As String Try Dim ...
Get Active Directory, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.