?
Solved

vbscript to copy a specific value from one attribute to another in Active Directory Users

Posted on 2013-05-13
3
Medium Priority
?
773 Views
Last Modified: 2013-06-06
Hello,
I'm looking for a way a vbscript can access the Active Directory, go through all the users one by one, and for each of them copy the values in the "Assistant" attribute (can by any other attribute for example) and copy them over to the "Fax" attribute.

Any such thing?
0
Comment
Question by:johnnyjonathan
  • 2
3 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39161128
if u want in powersehll, here it is.

Import-Module ActiveDirectory  
cls
$copyFromProp = 'Assistant'
$copyToProp = 'Fax'
Get-ADUser -Filter * -Properties $copyFromProp,$copyToProp | %{
	$user = $_
	$replaceHashTable = New-Object HashTable 
	$replaceHashTable.Add($copyFromProp, $copyToProp) 
	Set-ADUser 'm.rivkin' -Replace $replaceHashTable
}

Open in new window

0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 39161195
here in vb script:
'SearchAD.vbs
On Error Resume Next

sCopyPropFrom = "Assistant"
sCopyPropTo = "Fax"

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strTarget = "LDAP://" & strDNSDomain
wscript.Echo "Starting search from " & strTarget

' Connect to Ad Provider
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCmd = CreateObject("ADODB.Command")
Set objCmd.ActiveConnection = objConnection 

objCmd.CommandText = "SELECT " & sCopyPropFrom & ", " & sCopyPropTo & " FROM '" & strTarget & "' WHERE objectCategory = 'user'"

Const ADS_SCOPE_SUBTREE = 2
objCmd.Properties("Page Size") = 100
objCmd.Properties("Timeout") = 30
objCmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCmd.Properties("Cache Results") = False

Set objRecordSet = objCmd.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	sCopyPropFromVal = objRecordSet.Fields(sCopyPropFrom)
	sCopyPropToVal = objRecordSet.Fields(sCopyPropTo)
	Set objUser = GetObject ("LDAP://" & sCopyPropFromVal)
	wscript.Echo objUser.Name & " - Assign " & sCopyPropFromVal & " to property " & sCopyPropTo
	objUser.Put sCopyPropTo, sCopyPropFromVal
	objUser.SetInfo
	objRecordSet.MoveNext
Loop

Open in new window

0
 

Author Closing Comment

by:johnnyjonathan
ID: 39225460
Works like a charm! thanks a lot!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

839 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