# --------------------------------------------------------------
# Relevante OU filtern und fuer jede OU eigene Datei generieren
# --------------------------------------------------------------
function prepareADObjectsByType {
    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [String] $typeTag
    )
    $adGroupObjects = @()
    $counter = 1
    $count = $ADObjects.Count
    foreach ($adObject in $ADObjects) {
        if (-Not ($adObject.Description)) {
            $adObject.Description = $Config.EmptyDescription
        }
        #$adObject.Description = $Config.EmptyDescription
        #$activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
        #Write-Host $activityMessage

        $currentADObject = $null
        if ($typeTag -ieq $FI.TypeTagAppConfiguration) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.AppConfiguration.Filter) {
                # $userMemberShips = getAllUserMemberships $adObject
                $userMemberShips = @()
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.AppConfiguration.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adObjectFileEntry = $adObject.ObjectName + ";" + $adObject.OU + ";" + $FI.RelevantGroups.AppConfiguration.ShortCut + ";" + $adObject.Description
                $adObjectFileEntry | out-file $AppConfigurationTmpFilePath  -Encoding "utf8NoBom" -Append
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) ) {
                    break
                }
                # $activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
                # Write-Host $activityMessage
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagAppAccess) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.AppAccess.Filter) {
                # $userMemberShips = getAllUserMemberships $adObject
                $userMemberShips = @()
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.AppAccess.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adObjectFileEntry = $adObject.ObjectName + ";" + $adObject.OU + ";" + $FI.RelevantGroups.AppAccess.ShortCut + ";" + $adObject.Description
                $adObjectFileEntry | out-file $AppAccessTmpFilePath  -Encoding "utf8NoBom" -Append
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) ) {
                    break
                }
                # $activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
                # Write-Host $activityMessage
            }    
        }
        elseif ($typeTag -ieq $FI.TypeTagFileSystemAccess) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.FilesystemAccess.Filter) {
                # Teile gehoeren in den Block Anwendungszugriff
                # $userMemberShips = getAllUserMemberships $adObject
                $userMemberShips = @()
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.FilesystemAccess.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adObjectFileEntry = $adObject.ObjectName + ";" + $adObject.OU + ";" + $FI.RelevantGroups.FilesystemAccess.ShortCut + ";" + $adObject.Description
                $adObjectFileEntry | out-file $FilesystemAccessTmpFilePath  -Encoding "utf8NoBom" -Append
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) ) {
                    break
                }
                # $activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
                # Write-Host $activityMessage
            } 
        }
        elseif ($typeTag -ieq $FI.TypeTagOrganisation) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.Organisation.Filter) {
                # $userMemberShips = getAllUserMemberships $adObject
                $userMemberShips = @()
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.Organisation.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adObjectFileEntry = $adObject.ObjectName + ";" + $adObject.OU + ";" + $FI.RelevantGroups.Organisation.ShortCut + ";" + $adObject.Description
                $adObjectFileEntry | out-file $OrganisationTmpFilePath  -Encoding "utf8NoBom" -Append
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) ) {
                    break
                }
                # $activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
                # Write-Host $activityMessage
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagSecurity) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.Security.Filter) {
                # $userMemberShips = getAllUserMemberships $adObject
                $userMemberShips = @()
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.Security.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adObjectFileEntry = $adObject.ObjectName + ";" + $adObject.OU + ";" + $FI.RelevantGroups.Security.ShortCut + ";" + $adObject.Description
                $adObjectFileEntry | out-file $SecurityTmpFilePath  -Encoding "utf8NoBom" -Append
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) ) {
                    break
                }
                # $activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
                # Write-Host $activityMessage
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagRole) {
            #Write-Host "----: " $FI.TypeTagRole " -> " $typeTag " -> " $FI.RelevantGroups.Role.Filter " -> " $adObject.OU
            #-contains "find"

            #if ($adObject.OU -cmatch $FI.RelevantGroups.Role.Filter) {
            if ($adObject.OU -like "*Anwenderrollen*") {
                # $userMemberShips = getAllUserMemberships $adObject
                $userMemberShips = @()
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.Role.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adObjectFileEntry = $adObject.ObjectName + ";" + $adObject.OU + ";" + $FI.RelevantGroups.Role.ShortCut + ";" + $adObject.Description
                $adObjectFileEntry | out-file $RoleTmpFilePath  -Encoding "utf8NoBom" -Append
                $adGroupObjects += $adObjectEntry
                $counter++  

                #Write-Host "+---: " $FI.TypeTagRole " -> " $typeTag " -> " $counter

                if ($IsTestMode -and ($counter -gt $Config.TestMode) ) {
                    break
                }
                #$activityMessage = "Processing entry " + $counter + " of " + $count + ", type " + $typeTag + " object " + $adObject.ObjectName
                #Write-Host $activityMessage
            }
        }

        # $percentCompleted = $counter / $count * 100
        # Write-Progress -Activity $activityMessage -Status "zzz" -PercentComplete -1
    }
    return $adGroupObjects
}
