ForEach iteration limit?

Good Evening,

I have a weird issue that I am reaching out to Reddit for assistance with.

I have a script in which I grab every group in AD and pipe it into a ForEach block. Everything works great except some groups aren’t being processed in the ForEach block.

What I did to test to make sure that the command Get-ADGroup was working as intended – I ran the command (Get-ADGroup -Filter “*” -ResultPageSize 2220 | Out-File C:scriptstest.txt) and piped the output to a text file. This text file gave me every group with the default information for each group as expected (Note that there is only about 1000 total groups), example:

DistinguishedName : CN=DHCP Administrators,XXXX GroupCategory : Security GroupScope : DomainLocal Name : DHCP Administrators ObjectClass : group ObjectGUID : fac70db6-XXXX SamAccountName : DHCP Administrators SID : S-1-5-21-XXXX 

So now that I know Get-ADGroup is working properly, i’m wondering if the ForEach isn’t working. I went ahead and tested by putting into the ForEach block Add-Content to output each groups name into a textfile. It ended up missing some groups. These groups also don’t change every time I run the ForEach block, it’s the same groups every time – until I piped my original Get-ADGroup command to a Sort-Object command (Which changes the order in which groups are thrown into the ForEach). However, I believe now I have only shifted the problem to a different set of groups.

Back to the original text file I made, simply by outputting all groups – I noticed that the groups that don’t get counted, are all the groups that exceed line 9000 in the text file (9000 because each group creates roughly 9ish lines from the information it pulls by default).

Full Code that should re-produce the problem (Have over I would say 1100 AD groups):

Get-ADGroup -Filter "*" -ResultPageSize 2200 | Out-File C:Scriptstest.txt Get-ADGroup -Filter "*" -ResultPageSize 2200 | ForEach { $groupName = $ Add-Content C:Scriptstest.txt "$groupname `n" } 

Has anyone ever come across this before? It’s a very odd occurrence… Let me know if I need to test or provide further information – any help is greatly appreciated 🙂

submitted by /u/konigpyro
[link] [comments]

Leave a Reply