Re: Moving user to another container

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

The main thing is you need to bind to the user object, objUser. Also, you
should refer to objUser instead of oUser when you add the user to the group.

A less important point is that I would test if the user is already a member
of the group before adding them. A minor point is that I would bind to the
group object outside the loop, so it is bound once rather than repeatedly.
I've added to your script below:

Richard,
I guess I do need your script because I still want to use the csv file.
So I've modified your script to change the profile path and to add the
users to a security group. Would you mind checking it over please and
letting me know if the scirpt makes sense? Thanks,
==================
Option Explicit
Dim strFile, strOU, objOU, objRootDSE, strDNSDomain
Dim objTrans, strNetBIOSDomain, strNTName, strUserDN

Dim objFSO, objFile


' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify the csv file. This file has one user NT name per line.

strFile = "c:\scripts\students.csv"

' Bind to group object once, outside the loop where we read names.
Set grp =
GetObject("LDAP://cn=gg047-users,dc=student,dc=cssd,dc=ab,,dc=ca";)



' Determine DNS name of domain from RootDSE.
Set objRootDSE = GetObject("LDAP://RootDSE";)
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use the NameTranslate object to find the NetBIOS domain name from the
' DNS domain name.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.

objTrans.Init ADS_NAME_INITTYPE_GC, ""

' Use the Set method to specify the RPC 1779 format of the domain name.
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain

' Use the Get method to retrieve the NetBIOS Name.

' This has a trailing backspace, which we will use.
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)

' Open the file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, 1)



' Read each line of the file and process.

Do Until objFile.AtEndOfStream
strNTName = Trim(objFile.ReadLine)
If (strNTName <> "") Then
' Use the Set method to specify the NT name.
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & strNTName

' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Change the profile attribute to the appropriate path

' These four lines were in the script center example, but I don't think I
need them?
' strCurrentProfilePath = objUser.Get("profilePath")
' intStringLen = Len(strCurrentProfilePath)
' intStringRemains = intStringLen - 11
' strRemains = Mid(strCurrentProfilePath, 12, intStringRemains)

' I agree, these lines are not needed.
' Bind to the user object, using strUserDN.
Set objUser = GetObject("LDAP://"; & strUserDN)


strNewProfilePath = "\\s0470s0371-01\students2.man$"
objUser.Put "profilePath", strNewProfilePath
objUser.SetInfo

' Add users to the required 047 groups
' I'm assuming the first line I copied from the sample script is the AD
path to the security group?


' Group object bound above. I removed "Set grp" statement here.

' Add user to group only if they are not already a member.
' There is no need for the SetInfo statement.
If Not grp.IsMember(objUser.AdsPath) Then
grp.Add(objUser.AdsPath)
End If


End If

Loop

' Clean up.

objFile.Close
==================

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.



Relevant Pages

  • Re: Moving user to another container
    ... finished, I can run the same script, substituting the remove user command ... and just leave the profile path blank to clear the profile path (I seem to ... ' Bind to group object once, outside the loop where we read names. ... ' Use the Set method to specify the RPC 1779 format of the domain name. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Help with script to add multiple users to multiple groups
    ... Has anyone got a script that will allow me to add all users from one ... use the Add method of the group object to add each user to the group. ... Dim objOU, objGroup, objUser ... ' Bind to the OU. ...
    (microsoft.public.scripting.vbscript)
  • Re: Help with script to add multiple users to multiple groups
    ... thanks for script you have on your website to import users ... the group object to check if each user is already a member of the group, ... Dim objOU, objGroup, objUser ... ' Bind to the OU. ...
    (microsoft.public.scripting.vbscript)
  • Re: Help with script to add multiple users to multiple groups
    ... thanks for script you have on your website to import users from ... then use the Add method of the group object to add each user to the group. ... Dim objOU, objGroup, objUser ... ' Bind to the OU. ...
    (microsoft.public.scripting.vbscript)
  • Re: error checking
    ... Using "On Error Resume Next" throughout a script masks all errors, ... each computer (no need to repeat the bind operation). ... ' Add the domain user to the local group. ... If the user object does not ...
    (microsoft.public.windows.server.scripting)