No announcement yet.

Logon script- Code not cycling thru a users group membership

  • Filter
  • Time
  • Show
Clear All
new posts

  • Logon script- Code not cycling thru a users group membership

    H ave the code below which connects printers for the user when run. For some reason, it only recognises the first group membership then drops out of the For Next loop without cycling thru the other groups:
    Private Function ConnectGroupPrinters (objLogFileName, strUserNameADsPath)
    Dim objUser
    Dim objGroup
    Dim intPrinters
    Dim strGroupName
    Dim strServer
    Dim intServerStartPos
    Dim strPrinter
        ' On Error resume Next
    	intRC = WriteToFile (objLogFileName,  "+-- ConnectGroupPrinters [Started] --+")
        intRC = WriteToFile (objLogFileName, "| Connect Group Printers for'" & strUserName & "'" )
        Set objUser = GetObject(strUserNameADsPath)
        If Err.Number Then
           intRC = ErrorHandler (objLogFileName, "ConnectGroupPrinters", Err, boolErrorPopup)
           Exit Function
        End If
    	WScript.StdOut.WriteLine objUser.Groups(0)
    	WScript.StdOut.WriteLine objUser.Groups(1)
    	WScript.StdOut.WriteLine objUser.Groups(2)
    	intPrinters = 0
        For Each objGroup In objUser.Groups
    		strGroupName = objGroup.Get("Name")
    		If Left(strGroupName,4) = "gPrn" Then
    			If Right(strGroupName,3) = "Map" Then
    				intServerStartPos = InStr(6,strGroupName,"_",1)
    				strServer = Mid (strGroupName,6,intServerStartPos-6)				
    				strPrinter = Left ( Mid(strGroupName,intServerStartPos+1), Len(Mid(strGroupName,intServerStartPos+1))-4)
                   	intRC = WriteToFile (objLogFileName, "| ==> Member of Printer Group'" & strGroupName & "'" & _
                   										" [" & "\\" & strServer & "\" & strPrinter & "]")
    				intRC = ConnectWindowsNetworkPrinter (objLogFileName, "\\" & strServer & "\" & strPrinter, False)
    			    If Err.Number Then
    			       objShell.Popup "Error connecting group printers. " & vbNewLine & "[" & Err.Number & "]" & _
    			       					vbNewLine & "[" & Err.Description & "]" & _
    			       					vbNewLine & "[" & Err.source & "]"
    			       intRC = ErrorHandler (objLogFileName, "ConnectGroupPrinters", Err, boolErrorPopup)
    			       Exit Function
    			    End If
    				intPrinters = intPrinters + 1
    			End If
    		End If
        intRC = WriteToFile (objLogFileName, "| Member of " & intPrinters & " groups.")
        ConnectGroupPrinters = intPrinters
    	intRC = WriteToFile (objLogFileName,  "+-- ConnectGroupPrinters [Ended  ] --+")
    End Function
    I'm using myself as the test user, and I am a member of many AD groups, but for some reason, the "For Each objGroup In objUser.Groups" loop only recognises my membership to "Enterprise Admins", gets to the Next then continues down the function and finally exits. It never cycles thru all my group memberships.

    Can anyone suggest why this is happening? How do I tell how many objects are in the object "objUser.Groups"?
    +-- JDMils
    +-- Regional Systems Engineer, DotNet programmer & Jack of all trades

  • #2
    Re: Logon script- Code not cycling thru a users group membership

    the objUser.Groups should cycle through all existing groups (not including your primary group). It may be possible that the function exists at the 'Exit Function' line. BTW, what type of groups are you missing? (Global, Local, Universal).

    Anyway, I suggest using the 'TokenGroups' and (if needed) 'tokenGroupsGlobalAndUniversal' these calculated attributes would do the recursion for you and would return nested groups as well (instead of writing the recursion yourself).

    My preferred method of retrieving group membership is quering the registry for token groups and resolving the SIDs. This is my code for that (I'm planning on writing an article about that that also explains the code in detail.)

    If you have any questions, feel free to contact me.

    const REG_PATH_GROUPS = "HKCU\Software\Microsoft\Windows\CurrentVersion\Group Policy\GroupMembership\"
    const REG_PATH_ISTS = "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\TSAppCompat"
    const FILE_PATH = "d:\asaf\SmartX\Customers\AIG\login.csv"
    set oShell = wscript.createobject("")
    set oNet = wscript.createobject("")
    Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
    dim arrGroups() 'Array of strings - contains all groups
    dim nGroups ' int contains number of groups
    dim iGroups
    for iGroups = 0 to nGroups -1
     msgbox arrGroups(iGroups) 
    function IsInGroup(sGroup)
     on error resume next
     for iGroups = 0 to nGroups-1
      if lcase(sgroup) = lcase(arrgroups(iGroups)) then
       isingroup = true
       exit function
      end if
     isingroup = false
    end function
    function GetGroups()
     on error resume next
     n = oshell.regread(REG_PATH_GROUPS & "Count")
     for iGroups = 0 to n-1
      sGroupSID = oshell.regread(REG_PATH_GROUPS & "Group" & iGroups)
      if len(sGroupSID) > 12 then
       redim preserve arrGroups(nGroups)
       Set objSID = oWMIService.Get("Win32_SID='" & sGroupSID & "'")
       arrGroups(nGroups) =  objSID.AccountName
       nGroups = nGroups +1
      end if
     if err.number <> 0 then isingroup = 1
    end function
    Edit by Dumber: Wrapped code tags around the script.
    Last edited by Dumber; 25th December 2008, 12:01.