We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


VBS Add user to groups

Medium Priority
Last Modified: 2012-05-06
Im trying to do a script that adds a new created user to certain groups depending of the vlaue of the title, Department & Office attribute in active directory. Its working fine when i run it from console, using inputbox as input. But my goal is o run the script with admanager (web based administration utility).  So i change the line with inpubox to Arguments(0).

Dim username
username = Wscript.Arguments(0)

And runs the following script from Admanager.
cscript \\server\share\%sample.vbs %username%

Then i get the error
Object doesn't support this property or method: 'objGroup.add'

Can someone explain why this dosent work.

Best Regards
strFileserver = "\\vkb200file\user"
'strScanserver = "\\vkb200scan\scan"
strGrpOU = "OU=Groups,OU=Common,OU=Vinge," ' Sets Group
username = InputBox ("Write username::") 'Change to command arguments
strUserDN = SearchDistinguishedName(username)
WScript.Echo (strUserDN)
'  Bind to Active Directory and get LDAP name
Set objRootLDAP = GetObject("LDAP://RootDSE")
strDNSDomain = objRootLDAP.Get("DefaultNamingContext")
Set WshShell = CreateObject("WScript.Shell")
' Information från AD
Set objUser = GetObject("LDAP://" & strUserDN)
strUserTitle = objUser.title
strUserDepartment = objUser.department
strCompany = objUser.company
strOffice = objUser.physicalDeliveryOfficeName
'Wscript.echo "test: " & strUserDN
'WScript.Echo "Username: " & objUser.cn
'WScript.Echo "Username: " & objUser.SamaccountName
'WScript.Echo "Title: " & objUser.title
'WScript.Echo "Department: " & objUser.department
'WScript.Echo "Company: " & objUser.company
'WScript.Echo "Manager: " & objUser.manager
'WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
' Sets Location Group
Select Case strOffice
Case "Stockholm"
strGroup = "CN=Testgrupp A," ' Sets GroupName
Case "Göteborg"
strGroup = "CN=Testgrupp B," ' Sets Groupname
Set objGroup = GetObject("LDAP://"& strGroup _
& strGrpOU & strDNSDomain)
' Create HomefOlder
strFolder = "tabort1"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("y:\"& strFolder) ' Byt strFolder mot username
Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet
    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

Open in new window

Watch Question

another example can be found here:

that might solve your problem, pls confirm.

good luck

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
kihl71System Engineneer


Thanks for helping out,
After reading the example i changed this, and it worked.

strUser = ObjUser.AdsPath
WScript.Echo strUser

Select Case strOffice

Case "Stockholm"
strGroup = "CN=Testgrupp A," ' Sätter GruppNamn

Case "Göteborg"
strGroup = "CN=Testgrupp B," ' Sätter GruppNamn
End Select

Set objGroup = GetObject("LDAP://"& strGroup _
& strGrpOU & strDNSDomain)

objGroup.add strUser
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.