Discussion:
Help with multiple Loops
(too old to reply)
p***@gmail.com
2014-05-02 16:37:11 UTC
Permalink
Can you take a look at this VBScript and help me figure out what I'm doing wrong.

It reads in two files, One has four users and the other has 45 group Names

It reads the users in first, then translates their NT name to a DN Name in an endofstream loop

Inside of that loop it starts another endofstream loop with the group names and adds the users to each group.

The issue that I'm having is it is only adding the first name listed in the username file. It IS adding that one user to ALL the group.

I have a writeline within the loop to see if it going through all of the names and it is writing all four names out to the file.

What am I missing here?


'Script begins here
Dim objGroup, objUser, objFSO, objFile, strDomain, strGroup, Domain, Group
'translate settings
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_PROPERTY_APPEND = 3
Dim NameTranslate
Set NameTranslate = CreateObject("NameTranslate")

NameTranslate.Init ADS_NAME_INITTYPE_GC, ""

Dim NT4Name, DN

GrpWrite = ("C:\MScripts\JustGroupListadmin.txt")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile2 = objFSO.OpenTextFile(GrpWrite)
Set objFile = objFSO.CreateTextFile("C:\MScripts\Members1.txt")
Set objFile4 = objFSO.OpenTextFile("C:\MScripts\GroupInfo.txt")

'Change DomainName to the name of the domain the group is in

strDomain = "JUS"

'Change GroupName to the name of the group whose members you want to export


Do while NOT objFile2.AtEndOfStream

strGroup = objFile2.ReadLine

'translating NT name to DN
NT4Name = strDomain & "\" & strGroup
NameTranslate.Set ADS_NAME_TYPE_NT4, NT4Name
DN = NameTranslate.Get(ADS_NAME_TYPE_1779)

set UsertoAdd = GetObject("LDAP://" & DN)

'Pulling names of groups from txt and adding name translated above

Do while NOT objFile4.AtEndOfStream

AdminGroup = objFile4.ReadLine
Set DestGroup = Getobject("LDAP://CN=" & AdminGroup & ",OU=MOVEit,OU=COTS,OU=Groups,DC=EAS,DC=DS,DC=KY,DC=gov")
DestGroup.add(UsertoAdd.ADsPath)


Loop



objFile.WriteLine UsertoAdd.ADsPath


Loop



objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objUser = Nothing
Set objGroup = Nothing
Wscript.Echo "Done"
R.Wieser
2014-05-03 00:48:39 UTC
Permalink
Hello <noname>,
Post by p***@gmail.com
What am I missing here?
You only open objFile4 once, while you need to open and close it for each
loop of objFile2 (the outter loop). Otherwise each loop of objFile2 after
the the first one will find objFile4 at EOF.

In other words: you either need to open and close objFile4 *inside* the
objFile2 loop, or you need to rewind objFile4 just after it has encountered
its EOF (the first "loop" command).

Hope that helps

Regards,
Rudy Wieser
Post by p***@gmail.com
Can you take a look at this VBScript and help me figure out what I'm doing wrong.
It reads in two files, One has four users and the other has 45 group Names
It reads the users in first, then translates their NT name to a DN Name in
an endofstream loop
Post by p***@gmail.com
Inside of that loop it starts another endofstream loop with the group
names and adds the users to each group.
Post by p***@gmail.com
The issue that I'm having is it is only adding the first name listed in
the username file. It IS adding that one user to ALL the group.
Post by p***@gmail.com
I have a writeline within the loop to see if it going through all of the
names and it is writing all four names out to the file.
Post by p***@gmail.com
What am I missing here?
'Script begins here
Dim objGroup, objUser, objFSO, objFile, strDomain, strGroup, Domain, Group
'translate settings
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_PROPERTY_APPEND = 3
Dim NameTranslate
Set NameTranslate = CreateObject("NameTranslate")
NameTranslate.Init ADS_NAME_INITTYPE_GC, ""
Dim NT4Name, DN
GrpWrite = ("C:\MScripts\JustGroupListadmin.txt")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile2 = objFSO.OpenTextFile(GrpWrite)
Set objFile = objFSO.CreateTextFile("C:\MScripts\Members1.txt")
Set objFile4 = objFSO.OpenTextFile("C:\MScripts\GroupInfo.txt")
'Change DomainName to the name of the domain the group is in
strDomain = "JUS"
'Change GroupName to the name of the group whose members you want to export
Do while NOT objFile2.AtEndOfStream
strGroup = objFile2.ReadLine
'translating NT name to DN
NT4Name = strDomain & "\" & strGroup
NameTranslate.Set ADS_NAME_TYPE_NT4, NT4Name
DN = NameTranslate.Get(ADS_NAME_TYPE_1779)
set UsertoAdd = GetObject("LDAP://" & DN)
'Pulling names of groups from txt and adding name translated above
Do while NOT objFile4.AtEndOfStream
AdminGroup = objFile4.ReadLine
Set DestGroup = Getobject("LDAP://CN=" & AdminGroup &
",OU=MOVEit,OU=COTS,OU=Groups,DC=EAS,DC=DS,DC=KY,DC=gov")
Post by p***@gmail.com
DestGroup.add(UsertoAdd.ADsPath)
Loop
objFile.WriteLine UsertoAdd.ADsPath
Loop
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objUser = Nothing
Set objGroup = Nothing
Wscript.Echo "Done"
Dave "Crash" Dummy
2014-05-03 11:28:07 UTC
Permalink
Post by R.Wieser
Hello <noname>,
Post by p***@gmail.com
What am I missing here?
You only open objFile4 once, while you need to open and close it for each
loop of objFile2 (the outter loop). Otherwise each loop of objFile2 after
the the first one will find objFile4 at EOF.
In other words: you either need to open and close objFile4 *inside* the
objFile2 loop, or you need to rewind objFile4 just after it has encountered
its EOF (the first "loop" command).
Hope that helps
I would read both files into memory and create arrays. That would not
only simplify the code, it would likely greatly speed up operation.

Example code:
Set objFile2 = objFSO.OpenTextFile(GrpWrite)
data=objFile2.readAll
objFile2.close
File2array=split(data,vbCRLF)

Set objFile4 = objFSO.OpenTextFile("C:\MScripts\GroupInfo.txt")
data=objFile4.readAll
objFile4.close
File4array=split(data,vbCRLF)

for m=0 to ubound(File2array)

strGroup = File2array(m)

'process data

for n=0 to ubound(File4array)

AdminGroup = File4array(n)

'process data
next
next
--
Crash

"The future ain't what it used to be."
~ Yogi Berra ~
Loading...