
function analyzeADObjects
{
    param
    (
        [Parameter(Mandatory = $false, Position = 0)]
        [bool] $startParallel = $false        
    )
 
    $start = Get-Date
    outLog "started:  analyzeADObjects at" $start.ToString("HH:mm:ss.fff")

    $adObjectsOrganisation = @()
    $adObjectsAppAccess = @()
    $adObjectsFilesystemAccess = @()
    $adObjectsAppConfiguration = @()
    $adObjectsSecurity = @()

    if ($startParallel)
    {
        ### outLogVerbose 'if ($startParallel) -> true'
        Start-ThreadJob -ScriptBlock { $adObjectsOrganisation += analyzeADObjectsByType $FI.TypeTagOrganisation}
        Start-ThreadJob -ScriptBlock { $adObjectsAppAccess += analyzeADObjectsByType $FI.TypeTagAppAccess}
        Start-ThreadJob -ScriptBlock { $adObjectsFilesystemAccess += analyzeADObjectsByType $FI.TypeTagFilesystemAccess}
        Start-ThreadJob -ScriptBlock { $adObjectsAppConfiguration += analyzeADObjectsByType $FI.TypeTagAppConfiguration}
        Start-ThreadJob -ScriptBlock { $adObjectsSecurity += analyzeADObjectsByType $FI.TypeTagSecurity}    
        Get-Job | Wait-Job
    }
    else
    {
        ### outLogVerbose 'else -> true (if/elseif ($startParallel) -> false)'
        $adObjectsOrganisation += analyzeADObjectsByType $FI.TypeTagOrganisation
        $adObjectsAppAccess += analyzeADObjectsByType $FI.TypeTagAppAccess
        $adObjectsFilesystemAccess += analyzeADObjectsByType $FI.TypeTagFilesystemAccess
        $adObjectsAppConfiguration += analyzeADObjectsByType $FI.TypeTagAppConfiguration
        $adObjectsSecurity += analyzeADObjectsByType $FI.TypeTagSecurity    
    }
    outLog ("found " + $adObjectsOrganisation.Count + " entries of type " + $FI.TypeTagOrganisation)
    outLog ("found " + $adObjectsAppAccess.Count + " entries of type " + $FI.TypeTagAppAccess)
    outLog ("found " + $adObjectsFilesystemAccess.Count + " entries of type " + $FI.TypeTagFilesystemAccess)
    outLog ("found " + $adObjectsAppConfiguration.Count + " entries of type " + $FI.TypeTagAppConfiguration)
    outLog ("found " + $adObjectsSecurity.Count + " entries of type " + $FI.TypeTagSecurity)
    $end = Get-Date
    outLog ("finished: analyzeADObjects at " + $end.ToString("HH:mm:ss.fff") + ", used " + ($end - $start)) 
}

function analyzeDirectoryPaths
{
    $header = "Verzeichnisname", "Normierter Pfad" , "Kontrolle" , "Endpunktname", "Verzeichnisart", "Verantwortlicher (Stelle oder Personalnummer)" , "Gruppe Lesen", "Gruppe Aendern", "Benutzer mit Leserecht", "Benutzer mit Aenderungsrecht", "Rollen mit Leserecht", "Rollen mit Aenderungsrecht"
    $DirectoriesFilePathEntries = Get-Content -Path $DirectoriesInFilePath | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter ";" -Header $header
    
    $rootPath = [regex]::escape('\' + $SPK.RootDomainName + '.V998.INTERN\DFSRoot' + $Institut + '$\' + $Institut + '\Daten')
    
    outLog $rootPath
    foreach ($directoryEntry in $DirectoriesFilePathEntries)
    {
        outLogVerbose 'foreach $directoryEntry in $DirectoriesFilePathEntries:' $directoryEntry.Verzeichnisname
        $path = [regex]::escape($directoryEntry.Verzeichnisname)
        $restPath = $path.Replace($rootPath, "")
        outLog $restPath
    }
}

function analyzeADObjectsByType
{
    param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [String] $typeTag
    )
    $start = Get-Date
    outLog "started:  analyzeADObjects type " $typeTag "at" $start.ToString("HH:mm:ss.fff")

    $adGroupObjects = @()
    $counter = 1
    
    foreach ($adObject in $ADObjects)
    {
        outLogVerbose 'foreach $adObject in $ADObjects:' $adObject.ObjectName
        if (-not ($adObject.Description))
        {
            ### outLogVerbose 'if (-Not ($adObject.Description)) -> true'
            $adObject.Description = $Config.EmptyDescription
        }
        if ($typeTag -ieq $FI.TypeTagAppConfiguration)
        {
            ### outLogVerbose 'if ($typeTag -ieq $FI.TypeTagAppConfiguration) -> true'
            if ($adObject.OU -cmatch $FI.RelevantGroups.AppConfiguration.Filter)
            {
                ### outLogVerbose 'if ($adObject.OU -cmatch $FI.RelevantGroups.AppConfiguration.Filter) -> true'
                $userMemberShips = getAllUserMemberships $adObject
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.AppConfiguration.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) )
                {
                    ### outLogVerbose 'if ($IsTestMode -and ($counter -gt $Config.TestMode)) -> true'
                    break
                }
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagAppAccess)
        {
            ### outLogVerbose 'elseif ($typeTag -ieq $FI.TypeTagAppAccess) -> true'
            if ($adObject.OU -cmatch $FI.RelevantGroups.AppAccess.Filter)
            {
                ### outLogVerbose 'if ($adObject.OU -cmatch $FI.RelevantGroups.AppAccess.Filter) -> true'
                $userMemberShips = getAllUserMemberships $adObject
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.AppAccess.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) )
                {
                    ### outLogVerbose 'if ($IsTestMode -and ($counter -gt $Config.TestMode)) -> true'
                    break
                }
            }    
        }
        elseif ($typeTag -ieq $FI.TypeTagFileSystemAccess)
        {
            ### outLogVerbose 'elseif ($typeTag -ieq $FI.TypeTagFileSystemAccess) -> true'
            if ($adObject.OU -cmatch $FI.RelevantGroups.FilesystemAccess.Filter)
            {
                ### outLogVerbose 'if ($adObject.OU -cmatch $FI.RelevantGroups.FilesystemAccess.Filter) -> true'
                $userMemberShips = getAllUserMemberships $adObject
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.FilesystemAccess.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) )
                {
                    ### outLogVerbose 'if ($IsTestMode -and ($counter -gt $Config.TestMode)) -> true'
                    break
                }
            } 
        }
        elseif ($typeTag -ieq $FI.TypeTagOrganisation)
        {
            ### outLogVerbose 'elseif ($typeTag -ieq $FI.TypeTagOrganisation) -> true'
            if ($adObject.OU -cmatch $FI.RelevantGroups.Organisation.Filter)
            {
                ### outLogVerbose 'if ($adObject.OU -cmatch $FI.RelevantGroups.Organisation.Filter) -> true'
                $userMemberShips = getAllUserMemberships $adObject
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.Organisation.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) )
                {
                    ### outLogVerbose 'if ($IsTestMode -and ($counter -gt $Config.TestMode)) -> true'
                    break
                }
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagSecurity)
        {
            ### outLogVerbose 'elseif ($typeTag -ieq $FI.TypeTagSecurity) -> true'
            if ($adObject.OU -cmatch $FI.RelevantGroups.Security.Filter)
            {
                ### outLogVerbose 'if ($adObject.OU -cmatch $FI.RelevantGroups.Security.Filter) -> true'
                $userMemberShips = getAllUserMemberships $adObject
                $adObjectEntry = @{ObjectName = $adObject.ObjectName; Path = $adObject.OU; ShortCut = $FI.RelevantGroups.Security.ShortCut; Description = $adObject.Description; Users = $userMemberShips}
                $adGroupObjects += $adObjectEntry
                $counter++  
                if ($IsTestMode -and ($counter -gt $Config.TestMode) )
                {
                    ### outLogVerbose 'if ($IsTestMode -and ($counter -gt $Config.TestMode)) -> true'
                    break
                }
            }
        }
    }

    $end = Get-Date
    outLog "finished: analyzeADObjects type " $typeTag "at" $end.ToString("HH:mm:ss.fff") ", used " ($end - $start) 

    return $adGroupObjects
}

