synwave7
(Fresh Scripter)
2009-11-05 05:07 PM
Enumerate Group Membership using wild card

Can someone help me with this script. I suck at scripting so I snag everyone elses scripts and build them to suit me. I just haven't scripted in EONS so what would take me a week I can imagine would take one of the veterans two seconds to correct.

My question/need. Can someone edit this script to do...

I need to know (enumerate) all groups in my AD that start with (whatever) in this case APP-Citrix. Can I use a wild card in the script? In other words I want to create output that will output to a .txt or excel doc the membership of any group I choose.

In the pasted peice of script below...Can I use that * as a wild card so the script Lists the Group Name and who is in it I need or want to pipe the output to an Excel doc or at the least a .txt doc or .csv doc.

$members = groupmembers(@LDOMAIN, "APP-Citrix*")


SCRIPT I CUT AND PASTED FROM THIS SITE************STARTS BELOW******

 Code:
Break on

$members = groupmembers(@LDOMAIN, "Group1")

For Each $member in $members
	? $member
Next

Sleep 5


;Function Groupmembers($target, $group, optional $flag)
;NAME        GroupMembers
;
;ACTION      Returns an array of all group members of the specified group
;
;SYNTAX      GroupMembers(Target, Group, [FLAG])
;
;PARAMETERS  Target
;               The Domain name or Workstation to work with.  For faster workstation 
;               execution, include the Domain Name that the workstation is a meber of.
;
;               "Kixtart/beanbag" would be working with the workstation Beanbag in the 
;               Kixtart domain
;
;            Group
;               The Group you want to query
;
;            [FLAGS]
;               To use the flags options add the numbers of the desired flags toghthers and
;               Use that number in the flag field.
;
;                  Filter :(only one filter flag at a time please)
;                     1 = all
;                     2 = Users only
;                     4 = Groups only
;
;                  ADSI Information(return ADSI information "pick only one")
;                     8  = ADSPath field
;                     16 = ADSI Object Handle
;
;RETURNS     an array containing , if the ADSPath option is used the ADSPath 
;            will also be returned |.
;
;REMARKS     ADSI com object must be installed.
;
;EXAMPLES    ;this return all members of the Domain Admins group in the kixtart domain.
;            $members = groupmembers("kixtart","Domain admins")
;
;            ;this will will return all groups in the local administrators group on 
;            ;the Workstation beanbad in the kixtart domain.  Also the 
;            $groups = groupmembers("kixtart/beanbag","Administratoos","group")
Function Groupmembers($target, $group, optional $flag)
	Dim $temparray[8], $member, $i, $chunk, $flag, $ADSIFlag, $filterFlag
	$chunk = UBound($temparray)
	$flag = Val($flag)
	$i = 0
	$group = GetObject("WinNT://$target/$group")
	If VarType($group) <> 9 Exit(@error) EndIf Select
		Case $flag & 1
			$filterflag = 1
		Case $flag & 2
			$filterflag = 2
		Case $flag & 4
			$filterflag = 4
		Case 1
			$filterflag = 1
	EndSelect
	Select
		Case $flag & 8
			$ADSIFlag = 8
		Case $flag & 16
			$ADSIFlag = 16
	EndSelect For Each $member in $group.members
	Select
		Case $filterflag = 2 And $member.class = "user"
			If SubStr($member.name, Len($member.name), 1) <> "$"
				$temparray[$i] = $member.name
				Select
					Case $adsiflag = 8
						$temparray[$i] = $member.adspath
					Case $adsiflag = 16
						$temparray[$i] = $member
				EndSelect
				$i = $i + 1
			EndIf
		Case $filterflag = 4 And $member.class = "Group"
			If SubStr($member.name, Len($member.name), 1) <> "$"
				$temparray[$i] = $member.name
				Select
					Case $adsiflag = 8
						$temparray[$i] = $member.adspath
					Case $adsiflag = 16
						$temparray[$i] = $member
				EndSelect
				$i = $i + 1
			EndIf
		Case $filterflag = 1
			If SubStr($member.name, Len($member.name), 1) <> "$"
				$temparray[$i] = $member.name
				Select
					Case $adsiflag = 8
						$temparray[$i] = $member.adspath
					Case $adsiflag = 16
						$temparray[$i] = $member
				EndSelect
				$i = $i + 1
			EndIf
		Case $filterflag
			;bit bucket
	EndSelect
	If $i = UBound($temparray)
		ReDim preserve $temparray[UBound($temparray) + $chunk]
	EndIf
	Next
	If $i <> 0
		ReDim preserve $temparray[$i - 1]
		$groupmembers = $temparray
	EndIf
EndFunction 


synwave7
(Fresh Scripter)
2009-11-05 07:29 PM
Re: Enumerate Group Membership using wild card

So I did some testing of my own after following the directions on the site here. I added the Debug on script piece and I get the following error from my script.

ERROR : undefined variable [members]!
Script: h:\groupmembership.kix
Line : 9


Here's the whole code...can someone help I tried time and again but can't get this script to work...

 Code:
 Break on
Debug on

DIM $rc
$rc=SETOPTION('explicit','on')
$rc=SETOPTION('novarsinstrings','on')


$members = groupmembers("mydomainname","activesync")

For Each $member in $members
	? $member
Next

Sleep 5


;Function Groupmembers($target, $group, optional $flag)
;NAME        GroupMembers
;
;ACTION      Returns an array of all group members of the specified group
;
;SYNTAX      GroupMembers(Target, Group, [FLAG])
;
;PARAMETERS  Target
;               The Domain name or Workstation to work with.  For faster workstation 
;               execution, include the Domain Name that the workstation is a member of.
;
;               "Kixtart/beanbag" will produce information on the workstation "beanbag" in the 
;               "Kixtart" domain
;
;            Group
;               The Group you want to query
;
;            [FLAGS]
;               To use the flags options add the numbers of the desired flags toghthers and
;               Use that number in the flag field.
;
;                  Filter :(only one filter flag at a time please)
;                     1 = all
;                     2 = Users only
;                     4 = Groups only
;
;                  ADSI Information(return ADSI information "pick only one")
;                     8  = ADSPath field
;                     16 = ADSI Object Handle
;
;RETURNS     an array containing , if the ADSPath option is used the ADSPath 
;            will also be returned |.
;
;REMARKS     ADSI com object must be installed.
;
;EXAMPLES    ;this will return all members of the Domain Admins group in the kixtart domain.
;            $members = groupmembers("kixtart","Domain admins")
;
;            ;this will return all groups in the local administrators group on 
;            ;the Workstation beanbag in the kixtart domain.  Also the 
;            $groups = groupmembers("kixtart/beanbag","Administrators","group")

Function Groupmembers($target, $group, optional $flag)
	Dim $temparray[8], $member, $i, $chunk, $flag, $ADSIFlag, $filterFlag
	$chunk = UBound($temparray)
	$flag = Val($flag)
	$i = 0
	$group = GetObject("WinNT://$target/$group")
	If VarType($group) <> 9 Exit(@error) EndIf Select
		Case $flag & 1
			$filterflag = 1
		Case $flag & 2
			$filterflag = 2
		Case $flag & 4
			$filterflag = 4
		Case 1
			$filterflag = 1
	EndSelect
	Select
		Case $flag & 8
			$ADSIFlag = 8
		Case $flag & 16
			$ADSIFlag = 16
	EndSelect For Each $member in $group.members
	Select
		Case $filterflag = 2 And $member.class = "user"
			If SubStr($member.name, Len($member.name), 1) <> "$"
				$temparray[$i] = $member.name
				Select
					Case $adsiflag = 8
						$temparray[$i] = $member.adspath
					Case $adsiflag = 16
						$temparray[$i] = $member
				EndSelect
				$i = $i + 1
			EndIf
		Case $filterflag = 4 And $member.class = "Group"
			If SubStr($member.name, Len($member.name), 1) <> "$"
				$temparray[$i] = $member.name
				Select
					Case $adsiflag = 8
						$temparray[$i] = $member.adspath
					Case $adsiflag = 16
						$temparray[$i] = $member
				EndSelect
				$i = $i + 1
			EndIf
		Case $filterflag = 1
			If SubStr($member.name, Len($member.name), 1) <> "$"
				$temparray[$i] = $member.name
				Select
					Case $adsiflag = 8
						$temparray[$i] = $member.adspath
					Case $adsiflag = 16
						$temparray[$i] = $member
				EndSelect
				$i = $i + 1
			EndIf
		Case $filterflag
			;bit bucket
	EndSelect
	If $i = UBound($temparray)
		ReDim preserve $temparray[UBound($temparray) + $chunk]
	EndIf
	Next
	If $i <> 0
		ReDim preserve $temparray[$i - 1]
		$groupmembers = $temparray
	EndIf
EndFunction 


Arend_
(MM club member)
2009-11-05 09:05 PM
Re: Enumerate Group Membership using wild card

Replace
 Code:
DIM $rc

with
 Code:
DIM $rc, $member, $members


synwave7
(Fresh Scripter)
2009-11-05 09:21 PM
Re: Enumerate Group Membership using wild card

apronk,
Thanks for that. So, now I get this error which I'm not sure what it's telling me? How to fix it please?

ERROR : duplicate definition of variable [flag]!
Script : h:\groupmembership.kix
Line : 61


Sincerely,
Syn


Glenn BarnasAdministrator
(KiX Supporter)
2009-11-05 09:25 PM
Re: Enumerate Group Membership using wild card

 Code:
Function Groupmembers($target, $group, optional $flag)
	Dim $temparray[8], $member, $i, $chunk, $flag, $ADSIFlag, $filterFlag
is wrong.. the $flag is being passed to the function, which implicitly declares it. It's being declared again on the Dim line that follows. Simply delete $flag from that Dim line. (and the preceeding or followign comma!)

Glenn


synwave7
(Fresh Scripter)
2009-11-05 09:49 PM
Re: Enumerate Group Membership using wild card

Glenn,
My hat is off to you sir, thanks very much for your help as well as others! It works quite well now. This site is golden. People like you are of the same metal ;-)

Scripting is so far out of my forte although I have said I need to learn this better and vbscript better for about 8 yrs now. Drag, drag, drag my feet...

Sincerely,
Syn


synwave7
(Fresh Scripter)
2009-11-05 10:23 PM
Re: Enumerate Group Membership using wild card

So, now I pipe the output to a file correct? >c:\filename

I hate to be greedy but is there a way to add to this script so that it outputs the first and last name of the user and the FDQN.

In other words...The script spits out all users AD names for the output...

DoeJohn
DoeJane
DoeKelly
DoeBobby

And thats it. Does what it was built to do I suppose...but I also need the First and Last name and what OU the accoutn resides in?

DoeJohn, CN=domain, ou=whatever sub ou, ou=company name, ou=com

Is the above possible and would someone care to add it to the script for me. I looked on the site here at the UDF Library and I think I see what I need to do but it will take me weeks and some expert here mere minutes I imagine. Thanks kindly for any help I can get here. I'm under the microscope here and trying to do what I said 8yrs ago. Get better at scripting (when I am not under the microscope though)

Thanks,
Syn


Glenn BarnasAdministrator
(KiX Supporter)
2009-11-06 02:03 AM
Re: Enumerate Group Membership using wild card

I have a "Scripting Fundamentals" doc that I use when I teach. It's on my web site.. Sadly, both webservers choked after a hotfix application yesterday, so we're offline for the moment. Keep an eye out for us to come back online this weekend. It's pretty basic and can apply to any language, but the exercises employ Kix.

Glenn


synwave7
(Fresh Scripter)
2009-11-06 02:43 PM
Re: Enumerate Group Membership using wild card

Glenn,
Sounds great. Did I miss the website URL? Anyway to lend a hand in the meantime to add those functions to the script? I tried last night but no good. I had to quite early last night though in perperation for the Anniversery weekend for me and the missus this weekend. I'd love to be able to tell my boss I can get him the data he is asking for today.

I could simply manually do it using dsquery and dsget commands. I have that figured out already and it actually works great it's just that I have to run the command line command like 100 times to capture each group membership I need. Any way like I said before I am not trying to be greety but I am caught between a rock and a hard place. Wanting to learn scripting better so I dont have to beg and not having the time just now or this minute.

Regards,
Syn