Help with a Logic Puzzle of my own Stupidity: If’s, Else’s, and Loops

Hello all,

For some this will be a nice PowerShell logic puzzle exercise. Firstly, the code:

$AllGGroups = Get-ADGroup -SearchBase "[OU Places]" -Filter {Name -like "EG*"} | select Name # Grabs Each EG Group Individually foreach($AllGGroup in $AllGGroups) { $Group = Get-ADGroup $AllGGroup.Name -Properties * $Person = $Group.Name -split "-" $FolderCheck = $Person[1] $GrabTLFs = Get-ChildItem "G:*" | select Name if($GrabTLFs -match $FolderCheck) { $TLF = $Person[1] $GroupName = $Person[2] $GroupACL = "ReadOnly" $DescriptiontoNotes = $Group.Description Write-Host "Yes TLF" $Group.Name <# Set-ADGroup $Group.Name -Replace @{info="$DescriptiontoNotes"} Start-Sleep 2 Set-ADGroup $Group.Name -Description "G:$TLF$GroupName-$GroupACL" #> } else { $TLFName = $GrabTLF.Name $Grab2LFs = Get-ChildItem "G:$TLFName*" | select Name $FolderCheck = $Person[2] if($Grab2LFs -match $FolderCheck) { $TLF = $Person[1] $2LF = $Person[2] $GroupName = $Person[3] $GroupACL = "ReadOnly" $DescriptiontoNotes = $Group.Description Write-Host "Yes 2LF" $Group.Name <# Set-ADGroup $Group.Name -Replace @{info="$DescriptiontoNotes"} Start-Sleep 2 Set-ADGroup $Group.Name -Description "G:$TLF$2LF$GroupName-$GroupACL" #> } else { $2ndName = $Grab2LF.Name $Grab3rds = Get-ChildItem "G:$TLFName$2ndName*" | select Name $FolderCheck = $Person[3] if($Grab3rds -match $FolderCheck) { $TLF = $Person[1] $2LF = $Person[2] $3LF = $Person[3] $GroupName = $Person[4] $GroupACL = "ReadOnly" $DescriptiontoNotes = $Group.Description Write-Host "Yes 3LF" $Group.Name <# Set-ADGroup $Group.Name -Replace @{info="$DescriptiontoNotes"} Start-Sleep 2 Set-ADGroup $Group.Name -Description "G:$TLF$2LF$3LF$GroupName-$GroupACL" #> } else { $WarningGroup = $Group.Name Write-Warning "Not Found $WarningGroup" $Group.Name | Out-File "C:TBDGDriveGroups.txt" -Append } } } } 

Now to understand my conundrum, this is what I am trying to do:

  1. The Groups are grabbed at the Start from a Specific OU (filters respectfully for my task)
  2. A Foreach loop is started to use each Group individually to go down the main script
  3. The Start of this comparison is twofold:
  • After the -split of the Group’s name [6], it compares it to the list of folders in G:* [10].
  • If the Group is found to be attached to one of these folders, it says "Yes TLF" $Group.Name [12-22]
  • if there is no match though, then the else takes over to check the 2LF [26-46], and if not that, the 3LF [47-67]

Now for the problem:

  • If it cannot be found in the TLF, it must go down a level. However, to check all the 2LF in each TLF, a foreach loop is required. To better Clarify:

[Line 8] $GrabTLFs = Get-ChildItem "G:*" | select Name [Line 28] $TLFName = $GrabTLF.Name [Line 29] $Grab2LFs = Get-ChildItem "G:$TLFName*" | select Name 
  • Before [28], a foreach loop is needed to loop through each TLF and then grab all the 2LF under the TLF. However, if it is found, it does not move on because it is stuck in the loop. This can also lead to repeat outputs if the Group is located in more then one area which is what I do not want.
  • foreach loop will be needed even further down to loop through each 2LF in each TLF to find all 3LF. This however, results in so many outputs of repetitive duplicates.
  • Why is this an issue? Because even if it finds it, it will continue to go down the rabbit hole where it might not find it, sending a warning to me that it does not exist into a text file. This will also provide future problems with AD which I cannot let happen. I want it to restart at the beginning of the first loop when the group is found and begin again with the next group.

Also to Quick Point out: I know some of these are missing “s” but I had been taking out and putting in foreach loops earlier so some of the residue is still there.

Thoughts?

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

Leave a Reply