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

Re: Cycle through variables by Richard

Richard
Fri Jun 09 11:05:17 CDT 2006

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



Re: Cycle through variables by Tim

Tim
Fri Jun 09 12:41:50 CDT 2006

That worked perfectly, Richard - Thanks a ton for clearing that up. I have
one last question that is still related to this issue, though. I'm
currently pulling the domain in (as seen below as domain.com) statically.
Is there a way to pull that attribute from the user object (I did try the
userprincipalname, but that's not necessarily what I'm looking for) or
specify another variable to go to the sub also?

For example, the alert txt would show this:
1. smithj (domain1.com)
2. admin (domain1.com)
3. paul (domain2.com
etc.

And the echo csv would show this:
domain1.com,,smithj
domain1.com,,admin
domain2.com,,paul

Andy idea on how to fit this in or send to the sub-routine you came up with?
--
Tim

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



Re: Cycle through variables by Tim

Tim
Fri Jun 09 14:50:17 CDT 2006

> Andy idea on how to fit this in or send to the sub-routine you came up
> with?

Nevermind, I figured it out. I can simply put the domain name into a
variable before each call command.

Thanks again.
--
Time



Re: Cycle through variables by Richard

Richard
Fri Jun 09 14:51:01 CDT 2006

Hi,

You can use either the distinguishedName or canonicalName attributes of the
user object, but in both cases you have to parse the value to extract the
name of the domain. Some users may not even have a value assigned to
userPrincipalName. A distinguishedName will be similar to:

cn=UserName,ou=Sales,dc=MyDomain,dc=com

the canonicalName would be:

MyDomain.com/Sales/UserName

The second seems easier to parse for the string MyDomain.com. Perhaps:
====================
Sub EnumGroup(strGroup)
Set objGroup = GetObject ("LDAP://" & strGroup)
For each objMember in objGroup.Members
i = i + 1
strDomain = objMember.canonicalName
strDomain = Left(strDomain, InStr(strDomain, "/") - 1)
AlertReportXX = i&". "&objMember.sAMAccountName & " (" & strDomain &
")"
objTextFile2.WriteLine(AlertReportXX)
EchoXX = strDomain & ",,"&objMember.sAMAccountName
objTextFile.WriteLine(EchoXX)
Next

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

"Tim Chin" <blank> wrote in message
news:%23lYVgw%23iGHA.4204@TK2MSFTNGP02.phx.gbl...
> That worked perfectly, Richard - Thanks a ton for clearing that up. I
> have one last question that is still related to this issue, though. I'm
> currently pulling the domain in (as seen below as domain.com) statically.
> Is there a way to pull that attribute from the user object (I did try the
> userprincipalname, but that's not necessarily what I'm looking for) or
> specify another variable to go to the sub also?
>
> For example, the alert txt would show this:
> 1. smithj (domain1.com)
> 2. admin (domain1.com)
> 3. paul (domain2.com
> etc.
>
> And the echo csv would show this:
> domain1.com,,smithj
> domain1.com,,admin
> domain2.com,,paul
>
> Andy idea on how to fit this in or send to the sub-routine you came up
> with?
> --
> Tim
>
> 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
>