Solved

VBScript to Edit Computer Attribute in Active Directory

Posted on 2012-03-20
6
486 Views
Last Modified: 2012-03-21
I need a VBScript that will edit the Active Directory attribute "extensionAttribute12" to equal "Test-PCS-07" when it has no value within an OU named "Computers" that is directly under the domain "Contoso".

Anyone have the script syntax that needs to be run? Thanks!
0
Comment
Question by:brendan-amex
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
yo_bee earned 500 total points
ID: 37745556
Try this. I used something similar a while back.
It may work for you.

Option Explicit
Const adStateOpen = 1

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

Dim objConn 'ADO Connection object
Dim objRS 'ADO Recordset object
Dim strAlt, strName, StrADs 
Dim objUser
 

Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADSDSOObject"
objConn.Open
If objConn.State = adStateOpen Then
WScript.Echo "Authentication Successful!"
Else
WScript.Echo "Authentication Failed."
WScript.Quit(1)
End If
Set objRS = objConn.Execute _
("<LDAP://DC=contoso,DC=local>;(&(objectCategory=computer)" _
& "(objectClass=computer));Name,ADsPath,extensionAttribute12;SubTree")
While Not objRS.EOF
strAlt = objRS.Fields.Item("extensionAttribute12").Value
strName = objRS.Fields.Item("Name").Value
strADs = objRS.Fields.Item("ADsPath").Value

If (strAlt) = vbNullString then
'Wscript.Echo strName  & _
'vbCrLF & strADs & _
 'vbCrLf & strAlt & vbCrLf
 wscript.echo strAlt
 Set ObjUser = getobject(strADs)
objuser.Put ADS_PROPERTY_UPDATE, "extensionAttribute12",  "Test-PCS-07"
objuser.SetInfo              ' Now the property has no value.

else

end IF
objRS.MoveNext
Wend
Set objRS = Nothing
objConn.Close	

Open in new window

0
 

Author Closing Comment

by:brendan-amex
ID: 37745576
Thank you, I'll try it!
0
 
LVL 22

Expert Comment

by:yo_bee
ID: 37745581
Thanks for accepting my solution untested.  You did not have to accept this as of yet for further questions and solutions
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 22

Expert Comment

by:yo_bee
ID: 37747213
I just tested the scripted and it did not work, but I have resolved it with the script below.

Option Explicit
Const adStateOpen = 1

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

Dim objConn 'ADO Connection object
Dim objRS 'ADO Recordset object
Dim strAlt, strName, StrADs 
Dim objUser
 

Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADSDSOObject"
objConn.Open
If objConn.State = adStateOpen Then
WScript.Echo "Authentication Successful!"
Else
WScript.Echo "Authentication Failed."
WScript.Quit(1)
End If
Set objRS = objConn.Execute _
("<LDAP://DC=contoso,DC=local>;(&(objectCategory=computer)" _
& "(objectClass=computer));Name,ADsPath,extensionAttribute12;SubTree")
While Not objRS.EOF
strAlt = objRS.Fields.Item("extensionAttribute12").Value
strName = objRS.Fields.Item("Name").Value
strADs = objRS.Fields.Item("ADsPath").Value

If (isNull(StrAlt)) = true then

Set ObjUser = getobject(strADs)
objuser.Put "extensionAttribute12",  "Test-PCS-07"
objuser.SetInfo              ' Now the property has no value.



end IF
objRS.MoveNext
Wend
Set objRS = Nothing
objConn.Close	

Open in new window

0
 

Author Comment

by:brendan-amex
ID: 37747236
Thank you. I accepted it because I'm thinking about using power shell so I might just not use VBScript at all but that's good to know the above works. Thanks!
0
 
LVL 22

Expert Comment

by:yo_bee
ID: 37747381
I think PS is probably a better way to go anyway.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question