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

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

    if ($startParallel) {
        #this stuff won´t work this way - missing globals !!!
        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 {
        $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.mmm") ", 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
    # \\V998DPVP.V998.INTERN\DFSRootP014$\P014\Daten\Ablage\GS\01010
    
    # use slashes '/' as directory seperator to avoid backslash '\'-handling with 
    # $rootPath = [regex]::escape('\' + $SPK.RootDomainName + '.V998.INTERN\DFSRoot' + $Institut + '$\' + $Institut + '\Daten')
    $rootPath = [regex]::escape('\' + $SPK.RootDomainName + '.V998.INTERN\DFSRoot' + $Institut + '$\' + $Institut + '\Daten')
    
    outLog $rootPath
    foreach ($directoryEntry in $DirectoriesFilePathEntries) {
        $path = [regex]::escape($directoryEntry.Verzeichnisname)
        $restPath = $path.Replace($rootPath, "")
        # $restPath = $path.Substring($rootPath.length)
        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.mmm")

    $adGroupObjects = @()
    $counter = 1
    
    foreach ($adObject in $ADObjects) {
        if (-Not ($adObject.Description)) {
            $adObject.Description = $Config.EmptyDescription
        }
        if ($typeTag -ieq $FI.TypeTagAppConfiguration) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.AppConfiguration.Filter) {
                $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) ) {
                    break
                }
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagAppAccess) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.AppAccess.Filter) {
                $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) ) {
                    break
                }
            }    
        }
        elseif ($typeTag -ieq $FI.TypeTagFileSystemAccess) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.FilesystemAccess.Filter) {
                $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) ) {
                    break
                }
            } 
        }
        elseif ($typeTag -ieq $FI.TypeTagOrganisation) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.Organisation.Filter) {
                $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) ) {
                    break
                }
            }
        }
        elseif ($typeTag -ieq $FI.TypeTagSecurity) {
            if ($adObject.OU -cmatch $FI.RelevantGroups.Security.Filter) {
                $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) ) {
                    break
                }
            }
        }
    }

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

    return $adGroupObjects
}

