[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript to Edit Computer Attribute in Active Directory

Posted on 2012-03-20
6
Medium Priority
?
493 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 24

Accepted Solution

by:
yo_bee earned 1500 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 24

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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
LVL 24

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 24

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Suggested Courses

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