functions/general/Load-AzurePIMConfiguration.ps1

function Load-AzurePIMConfiguration
{
    <#
        .SYNOPSIS
            Loads the JSON files from the activated configurations.
        .DESCRIPTION
            Loads the JSON files from the activated configurations.
            All object definitions are stored in runtime variables.
        .PARAMETER ReturnDesiredConfiguration
            Returns the desired configuration after loading from activated configurations.
    #>

    [CmdletBinding()]
    Param (
        [switch] $ReturnDesiredConfiguration,
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
    
    begin
    {
        function Register-Resources {
            Param (
                [object[]] $Resources,
                [string] $ResourceType
            )
            if ($Resources.Count -gt 0) {
                $Resources | Foreach-Object {
                    $resource = $_ | Add-Member -NotePropertyMembers @{sourceConfig = $configuration.Name} -PassThru | ConvertTo-PSFHashtable -Include $($script:supportedResources[$ResourceType]["registerFunction"].Parameters.Keys)
                    # Calls the Register-AzurePIM(.*) function
                    & $script:supportedResources[$ResourceType]["registerFunction"] @resource -Cmdlet $PSCmdlet
                }
            }
        }

        $configurationsToLoad = Get-AzurePIMActiveConfiguration
        $script:desiredConfiguration = @{}
    }
    process
    {
        # Register all resources
        foreach ($configuration in $configurationsToLoad) {
            Write-Progress -Id 0 -Activity "Activating $($configuration.Name)"
            
            foreach ($resourceType in ($script:supportedResources.GetEnumerator() | Sort-Object {$_.Value.weight})) {
                $resourceTypeName = $resourceType.Name
                Write-Progress -Id 1 -Activity "Loading $resourceTypeName" -Status "Starting" -PercentComplete 0
                
                if ($resourceType.Value["parentType"]) {
                    $resourceDirectory = "{0}\{1}\{2}" -f $configuration.Path, $resourceType.Value["parentType"], $resourceTypeName
                }
                else {
                    $resourceDirectory = "{0}\{1}" -f $configuration.Path, $resourceTypeName
                }
                
                if (-Not (Test-Path $resourceDirectory)) { continue; Write-Progress -Id 1 -Activity "Loading $resourceTypeName" -Completed }

                $counter = 0
                $definitionFiles = Get-ChildItem -Path $resourceDirectory -File -Filter "*.json" -Recurse
                $definitionFiles | ForEach-Object {
                    Write-Progress -Id 1 -Activity "Loading $resourceTypeName" -CurrentOperation "Reading file $($_.Name)" -PercentComplete (($counter / $definitionFiles.count) * 100)
                    $content = Get-Content $_.FullName -Encoding UTF8 | Out-String
                    #$content = Assert-TemplateFunctions -InputTemplate $content | ConvertFrom-Json
                    $content = $content | ConvertFrom-Json                
                    Register-Resources -Id 1 -Resources $content -ResourceType $resourceTypeName                    
                    $counter++
                }
                Write-Progress -Id 1 -Activity "Loading $resourceTypeName" -Completed
            }

            Write-Progress -Id 0 -Activity "Activating $($configuration.Name)" -Completed
        }
    }
    end
    {
        if ($ReturnDesiredConfiguration) {
            Get-AzurePIMDesiredConfiguration
        }        
    }
}