VBS Add user to groups

Posted on 2009-02-12
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 =

strOffice = objUser.physicalDeliveryOfficeName

'Wscript.echo "test: " & strUserDN

'WScript.Echo "Username: " &

'WScript.Echo "Username: " & objUser.SamaccountName

'WScript.Echo "Title: " & objUser.title

'WScript.Echo "Department: " & objUser.department

'WScript.Echo "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

Question by:kihl71
    LVL 17

    Accepted Solution

    another example can be found here:

    that might solve your problem, pls confirm.

    good luck

    Author Closing Comment

    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

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now