• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 822
  • Last Modified:

VBS Add user to groups

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

1 Solution
another example can be found here:

that might solve your problem, pls confirm.

good luck
kihl71System EngineneerAuthor Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now