Re: Script to add computers in the "Log On to" field of a user account

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



The following should accomplish what you need:

Const ADS_SCOPE_ONELEVEL = 1

Set oConn = CreateObject("ADODB.Connection")
Set oCommand = CreateObject("ADODB.Command")
oConn.Provider = "ADsDSOObject"
oConn.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConn

oCommand.Properties("Page Size") = 1000
oCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL

sOU = "'LDAP://ou=Workstations,dc=domain,dc=com'"
sWorkstations = ""

oCommand.CommandText = "SELECT Name, ADsPath FROM " & sOU & _ " WHERE objectCategory ='computer'" Set oRecordSet = oCommand.Execute
oRecordSet.MoveFirst

Do Until oRecordSet.EOF
If sWorkstations = "" Then sWorkstations = oRecordSet.Fields("Name").Value
Else
sWorkstations = sWorkstations & "," & oRecordSet.Fields("Name").Value
End If
oRecordSet.MoveNext
Loop

Set oGroup = GetObject("LDAP://cn=ChangeMe,ou=Groups,dc=domain,dc=com";)
For Each oUser in oGroup.Members
oUser.Put "userWorkstations", sWorkstations
oUser.SetInfo
Next

hth
Marcin
.