Solved

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

Posted on 2013-05-13
3
733 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:sedgwick
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:
sedgwick earned 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
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)

803 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