Visual Basic 2008 and Active Directory computer descriptionmodifications

B

brett

ok, it appears that i have run into a spot that i am not able to
figure out on my own. Let me first say that i have hobbled together
this SUB from things i found on the internet and tutorials. Here is my
issue...I can search AD for the description of the computer and
display it out but am unable to figure out if the

"
If dirEntry.Properties.Contains("description") Then
dirEntry.Properties("description")(0) = "Turek, Brett
- Dell Laptop - Modified by VB program"
Else
dirEntry.Properties("description").Add("Turek, Brett -
Dell Laptop - Modified by VB program")
End If
dirEntry.CommitChanges()
"

part will actually do what i want, change the description in AD of the
computer. Can anyone look this over and let me know if i am on the
right path? How about some examples of how to change things in AD?

*******************
Complete Code:
*******************
Imports System
Imports System.Management
Imports System.Windows.Forms
Imports System.DirectoryServices

Public Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button5.Click
'Rename Machine
'SHELL("NETDOM RENAMECOMPUTER /NewName:" + TextBox3.Text + " /
UserD:domain\username /PasswordD:password /Force /Reboot:300")

'Rename Description
Dim userID = "Domain\username"
Dim password = "PASSWORD"
Dim dirEntry As New DirectoryEntry("LDAP://
OU=Computers,OU=Abingdon,OU=MAR,OU=Customer,DC=us,DC=company,DC=com",
userID, password)
Dim mySearcher As DirectorySearcher = New
DirectorySearcher(dirEntry)

mySearcher.Filter = String.Format("(CN=computerName)")
mySearcher.PropertiesToLoad.Add("cn")
mySearcher.PropertiesToLoad.Add("description")

Dim resEnt As SearchResult
resEnt = mySearcher.FindOne

If resEnt Is Nothing Then
MsgBox("User not found")
Else
MsgBox("Found: " +
resEnt.GetDirectoryEntry().Properties("cn").Value + " :: " +
resEnt.GetDirectoryEntry().Properties("description").Value)
If dirEntry.Properties.Contains("description") Then
dirEntry.Properties("description")(0) = "Turek, Brett
- Dell Laptop - Modified by VB program"
Else
dirEntry.Properties("description").Add("Turek, Brett -
Dell Laptop - Modified by VB program")
End If
dirEntry.CommitChanges()

MsgBox(resEnt.GetDirectoryEntry().Properties("description").Value)
End If
End Sub
 
B

brett

I found this article that helped a little.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21699772.html

My new SUB looks like this and it works:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Rename Machine
'SHELL("NETDOM RENAMECOMPUTER /NewName:" + TextBox3.Text + " /
UserD:domain\username /PasswordD:password /Force /Reboot:300")

Try
'Rename Description
Dim userID = "domain\username"
Dim password = "password"
Dim dirEntry As New DirectoryEntry("LDAP://
OU=Computers,OU=Abingdon,OU=MAR,OU=Customer,DC=us,DC=company,DC=com",
userID, password)
Dim mySearcher As DirectorySearcher = New
DirectorySearcher(dirEntry)

mySearcher.Filter = String.Format("(CN=computerName)")
mySearcher.PropertiesToLoad.Add("cn")
mySearcher.PropertiesToLoad.Add("description")

Dim resEnt As SearchResult
resEnt = mySearcher.FindOne()

If resEnt Is Nothing Then
MsgBox("Computer not found")
Else
MsgBox("Found: " +
resEnt.GetDirectoryEntry().Properties("cn").Value + " :: " +
resEnt.GetDirectoryEntry().Properties("description").Value)
'dirEntry.Properties("description").Add("Turek, Brett
- Dell Laptop - Modified by VB program")
'dirEntry.CommitChanges()

'echo out after changes - doesnt work
'MsgBox("Change1: " +
resEnt.GetDirectoryEntry().Properties("description").Value)

'new commit change works
Dim newComputerDesc =
dirEntry.Children.Find("CN=computerName", "computer")
newComputerDesc.Properties("description").Value =
"Turek, Brett - Dell Laptop"
newComputerDesc.CommitChanges()

'echo out after changes
'MsgBox("Change2: " +
resEnt.GetDirectoryEntry().Properties("description").Value)
End If
Catch generatedExceptionName As Exception
MsgBox(generatedExceptionName)
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top