WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: wbrd on December 04, 2013, 07:16:23 AM

Title: Problem with array
Post by: wbrd on December 04, 2013, 07:16:23 AM
Hi
I am trying to query for all groups in an OU and pipe the output to array but when I try to retrieve the group names I get "uninitiallized array element". I can pipe the same output to a text file but I would like to use array. Also, (if possible) I would like to create a dynamic array instead of static array.
Can anyone help please....thanks

groups = ArrDimension(1000)
cmd = StrCat("/c C:\Windows\system32\dsquery.exe group ou=admgroups,dc=child,dc=mydomain,dc=com > %groups%")
RunWait("C:\Windows\system32\cmd.exe", cmd)
item = ItemExtract(1, groups[1], @TAB)
For x = 0 to 200
   If groups
Title: Re: Problem with array
Post by: Deana on December 04, 2013, 09:52:32 AM
Something happened while pasting the code to your forum post.

Check out the ArrInitialize function. It can be used to initialize all the elements of an array. ArrayRedim can be used to change array dimensions in-place.

Here is a simple example to resize an array:
Code (winbatch) Select

; Allocate an array with 6 elements.
array = ArrDimension( 6 )
ArrInitialize( array, 0 )
Pause( 'Array Element Count', ArrInfo( array, 1 ) )
; Resize the array
ArrayRedim( array, 10 )
Pause( 'Array Element Count', ArrInfo( array, 1 ) )
Title: Re: Problem with array
Post by: wbrd on December 05, 2013, 06:45:57 AM
I added the following at the top of the script

groups = ArrDimension(1000)
ArrInitialize(groups, 0)

But when I do Display(1, "Groups", groups[0]) I get "0"
If I pipe the same command to a text file I see all the groups in the text file. So question is "Can/Should I not output the result of dsquery to an array?"

cmd = StrCat("/c C:\Windows\system32\dsquery.exe group ou=admgroups,dc=child,dc=mydomain,dc=com > %groups%")
RunWait("C:\Windows\system32\cmd.exe", cmd)
Title: Re: Problem with array
Post by: td on December 05, 2013, 09:05:01 AM
Cmd.exe has no idea what a WIL array is so you can't redirect output to a WIL array simply by redirecting output from cmd.exe launched process.  You can use the ADSI extender's dsFindPath or dsGetChldPath to obtain a list of groups in a container.  You could then use the Arrayize function to create an array containing the list's items.
Title: Re: Problem with array
Post by: Deana on December 05, 2013, 09:16:04 AM
Quote from: wbrd on December 05, 2013, 06:45:57 AM
I added the following at the top of the script

groups = ArrDimension(1000)
ArrInitialize(groups, 0)

But when I do Display(1, "Groups", groups[0]) I get "0"
If I pipe the same command to a text file I see all the groups in the text file. So question is "Can/Should I not output the result of dsquery to an array?"

cmd = StrCat("/c C:\Windows\system32\dsquery.exe group ou=admgroups,dc=child,dc=mydomain,dc=com > %groups%")
RunWait("C:\Windows\system32\cmd.exe", cmd)

I now see that you are using the command shell to pipe the results. Unfortunately that piped output is not directly available to the WinBatch script as an array. You could pipe the result to a file then read the file using FileGet or ArrayFileGet.

OR you could use the ADSI Extender to handle it directly in WinBatch. The dsGetMemGrp function returns a delimited list containing the paths of all members of a group. http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WIL~Extenders/ADSI+dsGetMemGrp~Sample.txt

Title: Re: Problem with array
Post by: td on December 05, 2013, 10:00:01 AM
Quote from: Deana on December 05, 2013, 09:16:04 AM

I now see that you are using the command shell to pipe the results. Unfortunately that piped output is not directly available to the WinBatch script as an array. You could pipe the result to a file then read the file using FileGet or ArrayFileGet.

OR you could use the ADSI Extender to handle it directly in WinBatch. The dsGetMemGrp function returns a delimited list containing the paths of all members of a group. http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WIL~Extenders/ADSI+dsGetMemGrp~Sample.txt

The DsQuery as posted indicates that the OP is looking for groups in a container and not members in a group so the dsGetMemGrp would not be an appropriate choice to duplicate the DsQuery output. 
Title: Re: Problem with array
Post by: Deana on December 05, 2013, 10:34:44 AM
Ah yes Tony (clearly the cough meds have kicked in ). Here is a code sample from the tech database that uses the dsFindPath function you suggested:

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WIL~Extenders/ADSI+List~Groups~in~OU.txt

Undebugged code sample:

Code (winbatch) Select
AddExtender("wwads44i.dll")

GlobalDistributionGroup = 2
LocalDistributionGroup = 4
UniversalDistributionGroup = 8

ServerDn=dsGetProperty("LDAP://rootDSE", "serverName")
ServerName=ItemExtract(1, ServerDn, ",")
ServerName=ItemExtract(2, ServerName, "=")
ServerDn=dsGetProperty("LDAP://rootDSE", "defaultNamingContext")
ServerPath="LDAP://" : ServerName : "/" : ServerDN

;groups_in_OU = dsFindPath(ServerPath, "(&(objectCategory=group) (groupType=%GlobalDistributionGroup%))")
;groups_in_OU = dsFindPath(ServerPath, "(&(objectCategory=group) (groupType=%LocalDistributionGroup%))")
groups_in_OU = dsFindPath(ServerPath, "(&(objectCategory=group) (groupType=%UniversalDistributionGroup%))")


aMembers = ArrDimension(1000)
           
For a = 0 to ItemCount( groups_in_OU, @TAB )-1
   sGroupPath = ItemExtract( a+1, groups_in_OU, @TAB )
   aMembers[a] = sGroupPath
Next

;Save out to a file then display
;ArrayFilePut('c:\temp\Members.txt', aMembers )
;Run( 'c:\temp\Members.txt', '' )
exit