August 30, 2007 at 2:19 pm
· Filed under Visual Basic .NET
Small function to delete a user from Active Directory.
”’ <summary>
”’ A function that deletes a user from Active Directory
”’ </summary>
”’ <param name=”DirEntry”>The Directory Entry that specifies the OU that holds the user</param>
”’ <param name=”sCommonName”>The Common Name (CN) of the user to delete</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserDelete(ByVal DirEntry As DirectoryEntry, ByVal sCommonName As String) As Boolean
‘Returning false if something goes wrong
AdUserDelete = False
Try
‘Finding the user
Dim _User As DirectoryEntry = DirEntry.Children.Find(“CN=” & sCommonName, “User”)
‘Removing the user
DirEntry.Children.Remove(_User)
‘Committing the changes
DirEntry.CommitChanges()
‘Closing
DirEntry.Close()
‘Went well – returning true
AdUserDelete = True
Catch ex As Exception
‘Throwing an error
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink
August 30, 2007 at 1:37 pm
· Filed under Visual Basic .NET
This small function can be used to set the password for a specific user in the AD.
”’ <summary>
”’ Function that sets the password for a specific user
”’ </summary>
”’ <param name=”DirEntry”>The DirectoryEntry to use eq the user</param>
”’ <param name=”sPassword”>The password</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserSetPassword(ByVal DirEntry As DirectoryEntry, ByVal sPassword As String) As Boolean
‘Return false if something goes wrong
AdUserSetPassword = False
Try
‘Creating an object-”array” to store the password
Dim _Password As Object() = New Object() {sPassword}
‘Calling a method
DirEntry.Invoke(“SetPassword”, _Password)
‘Returning true
AdUserSetPassword = True
Catch ex As Exception
‘Throwing exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink
August 30, 2007 at 8:24 am
· Filed under Visual Basic .NET
To update a user property in your Active Directory (e.g. telephone, common name, description), you can use this function. The DirectoryEntry should point to the specific user that you wish to update.
”’ <summary>
”’ A function that set the property of an object in Active Directory
”’ </summary>
”’ <param name=”DirEntry”>The DirectoryEntry to use eq the user</param>
”’ <param name=”PropertyName”>The property to set</param>
”’ <param name=”PropertyValue”>The value to set to the property</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserSetProperty(ByVal DirEntry As DirectoryEntry, ByVal PropertyName As String, ByVal PropertyValue As String) As Boolean
AdUserSetProperty = False
‘Specifying Administrator Account
DirEntry.Username = My.Settings.AdAdminUserName
DirEntry.Password = My.Settings.AdAdminPassword
Try
‘If the property has a value
If Not PropertyValue Is Nothing Then
‘Does the property exist
If DirEntry.Properties.Contains(PropertyName) Then
‘Setting the new value
DirEntry.Properties(PropertyName)(0) = PropertyValue
Else
‘Creating the property if it does not exist
DirEntry.Properties(PropertyName).Add(PropertyValue)
End If
‘Applying the changes
DirEntry.CommitChanges()
‘Closing
DirEntry.Close()
End If
‘Returning
AdUserSetProperty = True
Catch ex As Exception
‘Returning
AdUserSetProperty = False
‘Throwing an error
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink