Re: Cycle through variables



Tim Chin wrote:

Is there a way to specify a list of say 20 variables in a script and run
the same command for all variables without copying pasting the command 20
times in a script? Below is an example of my current script - I'd really
like to simplify it to make future modifications much easier. It looks
like there are two options that may produce my desired end result - using
an array and/or using multiple instances of the same variable, for
example: Group(1) = group in domain1, Group(2) = group in domain2, etc.

Can anyone shed some light on how to cycle through many variables to the
same command?

Group01="cn=somegroup,ou=somou,dc=domain1,dc=com"
Group02="cn=somegroup,ou=somou,dc=domain2,dc=com"
(etc.)

Set objGroup = GetObject ("LDAP://"&Group01)
For each objMember in objGroup.Members
i = i + 1
AlertReportXX = i&". "&objMember.sAMAccountName&" (domain.com)"
objTextFile2.WriteLine(AlertReportXX)
EchoXX = "domain.com,,"&objMember.sAMAccountName
objTextFile.WriteLine(EchoXX)
Next

Set objGroup = GetObject ("LDAP://"&Group02)
For each objMember in objGroup.Members
i = i + 1
AlertReportXX = i&". "&objMember.sAMAccountName&" (domain.com)"
objTextFile2.WriteLine(AlertReportXX)
EchoXX = "domain.com,,"&objMember.sAMAccountName
objTextFile.WriteLine(EchoXX)
Next

(etc.)

wscript.echo i
wscript.quit

Hi,

You can use a subroutine. Call the subroutine once for each group
distinguished name. Define objTextFile and objTextFile2 outside of the Sub
so they have global scope. Since i is probably meant to be a cumulative
total, also give it global scope by declaring it outside the Sub.
========================
' Declare variables with global scope.
Dim objTextFile, objTextFile2, i
Set objTextFile = ...
Set objTextFile2 = ...
i = 0

Call EnumGroup("cn=somegroup,ou=somou,dc=domain1,dc=com")
Call EnumGroup("cn=somegroup,ou=somou,dc=domain2,dc=com")
'(etc.)

Wscript.Echo i
Wscript.Quit

Sub EnumGroup(strGroup)
Set objGroup = GetObject ("LDAP://"; & strGroup)
For each objMember in objGroup.Members
i = i + 1
AlertReportXX = i&". "&objMember.sAMAccountName&" (domain.com)"
objTextFile2.WriteLine(AlertReportXX)
EchoXX = "domain.com,,"&objMember.sAMAccountName
objTextFile.WriteLine(EchoXX)
Next

End Sub

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.



Relevant Pages

  • RE: Want to interactively run a shell script
    ... sub process_command { ... Want to interactively run a shell script ... Any opinion expressed in this e-mail is personal to the sender ...
    (perl.beginners)
  • Re: Newbie "undefined value"
    ... > used strict and -w. ... From that same command line, if I pass in parameters, ... > to print to a file and using parts of Stein's guestbook script. ... > sub write_datafile { ...
    (comp.lang.perl.misc)
  • Re: command for script
    ... I did not use the script for a while and now I am not able to find out ... That line above is, in fact, the command you should use to run your ... close OUT1; ... sub parse { ...
    (comp.lang.perl.misc)
  • Newbie "undefined value"
    ... From that same command line, if I pass in parameters, ... I thought "strict" would catch all ... I had a short script that printed to the browser. ... sub write_datafile { ...
    (comp.lang.perl.misc)
  • Re: Change modified date of msg file to email recieved date
    ... My next project is to write a script to move an email to the proper ... Move and RFI email to the RFI folder, Move a PR email to the PR ... Dim olkApp, objFSO, ObjTextFile ... Call LogOutput (objTextFile, ...
    (microsoft.public.scripting.vbscript)