Stuck, blocking some groups in if

Hi,
The below code is working but I’m currently looking for a better way and a shorter way. What the code does is that it’s skipping to remove an AD group if it’s in the $RestrictedGroups array and just moving to the next group that the user had selected in the UDTable.

@($CompTable.selectedRows.SamAccountName.ForEach( { 
            [bool]$RestrictedAddGroup = $false
            foreach ($grp in $RestrictedGroups) {
                if ($_ -like $grp) {
                    [bool]$RestrictedAddGroup = $true
                    break
                }
            }
            if ($RestrictedAddGroup -eq $true) {
                Show-UDToast -Message "Du har inte behörighet att ändra $($_)" -MessageColor 'red' -Theme 'light' -TransitionIn 'bounceInUp' -CloseOnClick -Position center -Duration 3000
                return
            }
            else {
                Add-ADGroupMember -Identity $_ -Members $Computer
                if ($ActiveEventLog -eq "True") {
                    Write-EventLog -LogName $EventLogName -Source "AddToGroup" -EventID 10 -EntryType Information -Message "$($User) did add $($Computer) to the group $($_)`nLocal IP:$($LocalIpAddress)`nExternal IP: $($RemoteIpAddress)" -Category 1 -RawData 10, 20 
                }
            }
        } ) )
Product: PowerShell Universal
Version: 2.7.3

Hey, you could use an -in operator rather than looping through each $restrictedgroups to find the one you need. Should also be faster if it’s a large array.

So something like this if you’re just after cleaner code:

            if ($_ -in $RestrictedGroups) {
                Show-UDToast -Message "Du har inte behörighet att ändra $($_)" -MessageColor 'red' -Theme 'light' -TransitionIn 'bounceInUp' -CloseOnClick -Position center -Duration 3000
                return
            }
            else {
                Add-ADGroupMember -Identity $_ -Members $Computer
                if ($ActiveEventLog -eq "True") {
                    Write-EventLog -LogName $EventLogName -Source "AddToGroup" -EventID 10 -EntryType Information -Message "$($User) did add $($Computer) to the group $($_)`nLocal IP:$($LocalIpAddress)`nExternal IP: $($RemoteIpAddress)" -Category 1 -RawData 10, 20 
                }
            }

Alternativly, it’s not cleaner code but it’s faster if you have a large array, the IndexOf() method:

           $index = $null
           $index = $RestrictedGroups.IndexOf($_)
            if ($index -ne -1) {
                Show-UDToast -Message "Du har inte behörighet att ändra $($_)" -MessageColor 'red' -Theme 'light' -TransitionIn 'bounceInUp' -CloseOnClick -Position center -Duration 3000
                return
            }
            else {
                Add-ADGroupMember -Identity $_ -Members $Computer
                if ($ActiveEventLog -eq "True") {
                    Write-EventLog -LogName $EventLogName -Source "AddToGroup" -EventID 10 -EntryType Information -Message "$($User) did add $($Computer) to the group $($_)`nLocal IP:$($LocalIpAddress)`nExternal IP: $($RemoteIpAddress)" -Category 1 -RawData 10, 20 
                }
            }
2 Likes