functions/general/Activate-AzurePIMConfiguration.ps1

function Activate-AzurePIMConfiguration
{
    <#
        .SYNOPSIS
            Activates already created AzurePIM configurations.
 
        .DESCRIPTION
            Activate configurations you want to apply to your tenant. An activated configuration will be considered when testing or invoking configurations.
 
        .PARAMETER ConfigurationPaths
            One or more paths to AzurePIM configuration directories can be provided.
 
        .PARAMETER Force
            Overwrite already loaded configurations.
 
        .PARAMETER DoNotLoad
            Do not load resource definitions after activating.
 
        .EXAMPLE
            PS> Activate-AzurePIMConfiguration -ConfigurationPaths "C:\Temp\SomeConfiguration"
             
            Activates the configuration in path C:\Temp\SomeConfiguration.
 
    #>

    [CmdletBinding()]
    Param (
        [string[]] $ConfigurationPaths,
        [switch] $Force,
        [switch] $DoNotLoad
    )
    
    begin
    {
        $configurationsToActivate = @()
        #region Prepare to activate
        foreach ($path in $ConfigurationPaths) {
            $configuration = [PSCustomObject] @{
                filePath = "{0}\configuration.json" -f $path
                directoryPath = $path
                alreadyActivated = $false
            }

            #region Check if configuration.json exists
            if (!(Test-Path $configuration.filePath)) {
                Stop-PSFFunction -String "AzurePIM.ConfigurationFileNotFound" -StringValues $configuration.filePath
                return
            }
            else {
                # Clean configuration path
                $configuration.filePath = Resolve-PSFPath -Provider FileSystem -Path $configuration.filePath -SingleItem
            }
            #endregion

            #region Load configuration.json
            $contentDummy = Get-Content $configuration.filePath | ConvertFrom-Json -ErrorAction Stop
            $contentDummy | Get-Member -MemberType NoteProperty | ForEach-Object {
                Add-Member -InputObject $configuration -MemberType NoteProperty -Name $_.Name -Value $contentDummy.$($_.Name)
            }
            $configuration.alreadyActivated = $script:activatedConfigurations.Name -contains $configuration.Name
            #endregion
            
            if (!$Force -and $configuration.alreadyActivated) {
                Write-PSFMessage -Level Warning -String "Activate-AzurePIMConfiguration.AlreadyActivated" -StringValues $configuration.Name, $configuration.filePath
            }

            $configurationsToActivate += $configuration
        }
        #endregion
        
        #region Check prerequisites
        foreach ($configuration in $configurationsToActivate) {            
            if ($configuration.Prerequisite.Count -gt 0) {                
                foreach ($prereq in $configuration.Prerequisite) {
                    $prereqMet = $false
                    if ($prereq.contains("||")) {                        
                        $prereq.split("||") | Foreach-Object {
                            if ($_.trim() -in $configurationsToActivate.Name -or $_.trim() -in (Get-AzurePIMActiveConfiguration).Name) { $prereqMet = $true }
                        }
                    }
                    else {
                        if ($prereq -in $configurationsToActivate.Name -or $prereq -in (Get-AzurePIMActiveConfiguration).Name) {
                            $prereqMet = $true
                        }
                    }
                    if (-Not $prereqMet) {
                        Stop-PSFFunction -String "AzurePIM.PrerequisiteNotActivated" -StringValues $configuration.Name, $prereq
                        return
                    }
                }
            }
        }
        #endregion
    }
    process
    {        
        if (Test-PSFFunctionInterrupt) { return }

        foreach ($configuration in $configurationsToActivate) {
            if ($Force -and $configuration.alreadyActivated) {
                Write-PSFMessage -Level Host -String "Activate-AzurePIMConfiguration.RemovingAlreadyLoaded" -StringValues $configuration.Name, $configuration.directoryPath -NoNewLine
                $script:activatedConfigurations = @($script:activatedConfigurations | Where-Object {$_.Name -ne $configuration.Name})
                Write-PSFHostColor -String ' [<c="green">DONE</c>]'
            }
            elseif (!$Force -and $configuration.alreadyActivated) {
                continue
            }

            Write-PSFMessage -Level Host -String "Activate-AzurePIMConfiguration.Activating" -StringValues $configuration.Name, $configuration.filePath -NoNewLine            
            $script:activatedConfigurations += $configuration | Select-Object Name, @{Name = "Path"; Expression = {Resolve-Path $_.directoryPath}}, Description, Author, Weight, Prerequisite
            Write-PSFHostColor -String ' [<c="green">DONE</c>]'
        }

        Write-PSFMessage -Level Host -String "Activate-AzurePIMConfiguration.Sort" -NoNewLine
        $script:activatedConfigurations = @($script:activatedConfigurations | Sort-Object Weight)
        Write-PSFHostColor -String ' [<c="green">DONE</c>]'
    }
    end
    {
        if (-Not $DoNotLoad) {
            Load-AzurePIMConfiguration -Cmdlet $PSCmdlet
        }
    }
}