Solved

VBScript to Edit Computer Attribute in Active Directory

Posted on 2012-03-20
6
479 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 21

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 21

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 21

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 21

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now