Page 1 of 1 1
Topic Options
#190470 - 2008-11-03 04:10 PM Nested groups
lawe009 Offline
Fresh Scripter

Registered: 2004-04-14
Posts: 36
Hi,
I´m having trouble with a script. I´m setting variables for printmapping and filemapping through the loginscript. However I found out that when I nest groups Ingrop won´t show the nested groups. I´ve tried to use the UDF Usergroups but I get a "Error in expression: this type of array not supported in expressions.!". What do I do wrong? I post the original script below. This works fine except for the nested groups. Is the Usergroups the UDF to use and how should I use it?

 Code:
$Index = 0
DO
    $Group = ENUMGROUP($Index)
    $Index = $Index + 1

IF INSTR($group, "Grp-site-") 
 $Rawsite = $group
$site = Split(Split($Rawsite, "\")[1], "-")[2]

Endif
UNTIL Len($Group) = 0

$Index2 = 0
DO
    $Group2 = ENUMGROUP($Index2)
    $Index2 = $Index2 + 1

IF INSTR($group2, "Grp-dep-") 
 $Rawdep = $group2
$dep = Split(Split($Rawdep, "\")[1], "-")[2]

Endif
UNTIL Len($Group2) = 0


Edited by lawe009 (2008-11-03 04:13 PM)

Top
#190471 - 2008-11-03 04:20 PM Re: Nested groups [Re: lawe009]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
The UserGroups() UDF returns an array so you need to do a for each next loop to view each element on its own.

To get nested groups there is the ListNestedGroups() UDF. It's a bit more complicated to use then the UserGroups UDF but it does work.

UDF Library » ListNestedGroups() - Query AD for all groups of an account + nested


Edited by Mart (2008-11-03 04:20 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#190488 - 2008-11-04 02:49 PM Re: Nested groups [Re: lawe009]
lawe009 Offline
Fresh Scripter

Registered: 2004-04-14
Posts: 36
Thanx for your answer Mart. I can´t get the UDF ListNestedGroups to work. I´m just trying to run through it but it fails with a "ERROR : Error in expression: this type of array not supported in expressions.!"
I´ve used another UDF Translatename to get the DN of the current user. What am I doing wrong? Se code below.

 Code:
$_Account = TranslateName (3, "", 3, "@LDomain\@userid", 1)
                ? "DN = " + $_Account[0]
$_Nested = 0
$Groups = ListNestedGroups($_Account,$_Nested) 

If @ERROR
'Unexpected error retrieving groups. ' + @ERROR + ' - ' + @SERROR ?
Else
If UBound($Groups) >= 0
For Each $Group In $Groups
? " Group :"+ $Group 
Next
EndIf
EndIf 


$_Account = TranslateName (3, "", 3, "@LDomain\@userid", 1)
                ? "DN = " + $_Account[0]

listnestedgroups($_Account)

Function ListNestedGroups($_Account,Optional $_Nested)
  Dim $_objUser, $_colGroups, $_objGroup, $_Grps, $_NFlag
  Dim $_W, $_Element, $_TempString, $_OD, $_CN, $_ERR
 
  ; init the vars 
  $ListNestedGroups = 0                          ; default return value if errors occur 
  $_Nested = Val($_Nested)                       ; force to numeric value 
  $_NFlag = IIf($_Nested = 1, ' <Nested>', '')   ; set the output message for nested groups 
 
  $_objUser = GetObject($_Account)               ; instantiate the object 
  $_ERR = Val('&' + Right(DecToHex(@ERROR), 4))  ; get last 4 nybbles (2 bytes) of the error code 
  If $_ERR Exit $_ERR EndIf
 
  $_colGroups = $_objUser.Groups                 ; get the collection 
 
  For Each $_objGroup in $_colGroups
 
    $_OD = GetNested($_objGroup)                 ; nested group name 
    $_CN = $_objGroup.CN                         ; parent group name 
 
    ; Write the nested group name (and optional NESTED tag) to the index file 
    If $_OD <> ''
      If InStr($_TempString, $_OD) = 0
         $_TempString = $_TempString + $_OD + $_NFlag + Chr(10)
      EndIf
    EndIf
 
    ; write the parent group name to the index file, unless in nested-only mode 
    If $_Nested < 2
      If InStr($_TempString, $_CN) = 0
         $_TempString = $_TempString + $_CN + Chr(10)
      EndIf
    EndIf
 
  Next
 
  ; enumerate the index and put the value(s) into an array 
  $_Grps = Split(Left($_TempString,Len($_TempString)-1), Chr(10))
 
  ; Return the array of groups, and exit with success 
  $ListNestedGroups = $_Grps
  Exit 0
EndFunction
 
; Sub-Function for returning nested groups 
Function GetNested($objGroup)
  Dim $_colMembers, $_strMember, $_strPath, $_objNestedGroup, $_ERR
  ; init the return value to a null string 
  $GetNested = ''
  ; get the collection 
  $_colMembers = $objGroup.GetEx("memberOf")
  ; enumerate the collection 
  For Each $_strMember in $_colMembers
    $_strPath = "LDAP://" + $_strMember
    $_objNestedGroup = GetObject($_strPath)
    $GetNested = $_objNestedGroup.CN
  Next
  Exit 0
EndFunction

Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)

   Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
   Dim $NameTranslate, $ReturnName, $Error, $ErrorText

   $Error = 0
   $ErrorText = ""
   $ReturnName = ""
   $NameTranslate = CREATEOBJECT ("NameTranslate")
   $Error = @error
   $ErrorText = @serror
   if $Error = 0
      $NameTranslate.Init ($InitType, $BindName)
      $Error = @error
      $ErrorText = @serror
      if $Error = 0
         $NameTranslate.Set ($LookupNameType, $LookupName)
         $Error = @error
         $ErrorText = @serror
         if $Error = 0
            $ReturnName = $NameTranslate.Get($ReturnNameType)
            $Error = @error
            $ErrorText = @serror
         endif
      endif
   endif
   $TranslateName = $ReturnName, $Error, $ErrorText
Endfunction


Edited by lawe009 (2008-11-04 02:50 PM)

Top
#190492 - 2008-11-04 05:40 PM Re: Nested groups [Re: lawe009]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
The UDF works fine - you've got three problems with your script.

First - a pair of problems.. You are not declaring variables, so - every variable you use are automatically declared as a global. You're then using the same variable names inside and outside of the UDF - a problem when they are globals.

Second - you're (correctly) displaying the DN as $Account[0], but you are passing the Array to the function. The array is looking for an account, not an array of data.

Third, in the TranslateName function, you're declaring the variables that are passed as args in the function. That's not correct.

Take a look at these changes, which work fine on my system:

Glenn


$Rc = SetOption('Explicit', 'On')
 
Dim $Account, $Nested, $Groups, $Group
 
 
$Account = TranslateName (3, "", 3, "@LDomain\@userid", 1)
"DN = " + $Account[0] ?
$Nested = 0
$Groups = ListNestedGroups($Account[0],$Nested) 
 
If @ERROR
  'Unexpected error retrieving groups. ' + @ERROR + ' - ' + @SERROR ?
Else
  If UBound($Groups) >= 0
    For Each $Group In $Groups
      ? " Group :"+ $Group 
    Next
  EndIf
EndIf 
 
 
$Account = TranslateName (3, "", 3, "@LDomain\@userid", 1)
"DN = " + $Account[0] ?
 
listnestedgroups($Account[0])
 
Function ListNestedGroups($_Account,Optional $_Nested)
  Dim $_objUser, $_colGroups, $_objGroup, $_Grps, $_NFlag
  Dim $_W, $_Element, $_TempString, $_OD, $_CN, $_ERR
 
  ; init the vars  
  $ListNestedGroups = 0                          ; default return value if errors occur  
  $_Nested = Val($_Nested)                       ; force to numeric value  
  $_NFlag = IIf($_Nested = 1, ' <Nested>', '')   ; set the output message for nested groups  
 
  $_objUser = GetObject($_Account)               ; instantiate the object  
  $_ERR = Val('&' + Right(DecToHex(@ERROR), 4))  ; get last 4 nybbles (2 bytes) of the error code  
  If $_ERR Exit $_ERR EndIf
 
  $_colGroups = $_objUser.Groups                 ; get the collection  
 
  For Each $_objGroup in $_colGroups
 
    $_OD = GetNested($_objGroup)                 ; nested group name  
    $_CN = $_objGroup.CN                         ; parent group name  
 
    ; Write the nested group name (and optional NESTED tag) to the index file  
    If $_OD <> ''
      If InStr($_TempString, $_OD) = 0
         $_TempString = $_TempString + $_OD + $_NFlag + Chr(10)
      EndIf
    EndIf
 
    ; write the parent group name to the index file, unless in nested-only mode  
    If $_Nested < 2
      If InStr($_TempString, $_CN) = 0
         $_TempString = $_TempString + $_CN + Chr(10)
      EndIf
    EndIf
 
  Next
 
  ; enumerate the index and put the value(s) into an array  
  $_Grps = Split(Left($_TempString,Len($_TempString)-1), Chr(10))
 
  ; Return the array of groups, and exit with success  
  $ListNestedGroups = $_Grps
  Exit 0
EndFunction
 
; Sub-Function for returning nested groups  
Function GetNested($objGroup)
  Dim $_colMembers, $_strMember, $_strPath, $_objNestedGroup, $_ERR
  ; init the return value to a null string  
  $GetNested = ''
  ; get the collection  
  $_colMembers = $objGroup.GetEx("memberOf")
  ; enumerate the collection  
  For Each $_strMember in $_colMembers
    $_strPath = "LDAP://" + $_strMember
    $_objNestedGroup = GetObject($_strPath)
    $GetNested = $_objNestedGroup.CN
  Next
  Exit 0
EndFunction
 
Function TranslateName($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
 
   ; Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType 
   Dim $NameTranslate, $ReturnName, $Error, $ErrorText
 
   $Error = 0
   $ErrorText = ""
   $ReturnName = ""
   $NameTranslate = CREATEOBJECT ("NameTranslate")
   $Error = @error
   $ErrorText = @serror
   if $Error = 0
      $NameTranslate.Init ($InitType, $BindName)
      $Error = @error
      $ErrorText = @serror
      if $Error = 0
         $NameTranslate.Set ($LookupNameType, $LookupName)
         $Error = @error
         $ErrorText = @serror
         if $Error = 0
            $ReturnName = $NameTranslate.Get($ReturnNameType)
            $Error = @error
            $ErrorText = @serror
         endif
      endif
   endif
   $TranslateName = $ReturnName, $Error, $ErrorText
Endfunction
 
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 476 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.053 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org