modules/Azure/Infrastructure/Private/InvokeCIEMParallelForEach.ps1

function Invoke-CIEMParallelForEach {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)]
        [object[]]$InputObject,

        [Parameter()]
        [int]$ThrottleLimit = $script:CIEMParallelThrottleLimitDiscovery,

        [Parameter(Mandatory)]
        [scriptblock]$ScriptBlock
    )

    begin {
        $items = [System.Collections.Generic.List[object]]::new()
    }

    process {
        foreach ($item in $InputObject) {
            $items.Add($item)
        }
    }

    end {
        if ($items.Count -eq 0) {
            return @()
        }

        $module = Get-Module Devolutions.CIEM
        if (-not $module) {
            throw 'Invoke-CIEMParallelForEach requires the Devolutions.CIEM module to be loaded.'
        }

        $modulePath = $module.Path
        $authSnapshot = $script:AuthContext
        $azureAuthSnapshot = $script:AzureAuthContext
        $scriptBlockText = $ScriptBlock.ToString()

        $items | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
            Import-Module $using:modulePath -Force

            $moduleInstance = Get-Module Devolutions.CIEM
            & $moduleInstance {
                param($authSnapshot, $azureAuthSnapshot)

                $script:AuthContext = @{}
                if ($authSnapshot) {
                    foreach ($key in $authSnapshot.Keys) {
                        $script:AuthContext[$key] = $authSnapshot[$key]
                    }
                }

                if ($azureAuthSnapshot) {
                    $ctx = [CIEMAzureAuthContext]::new()
                    foreach ($property in 'ProfileId', 'ProfileName', 'ProviderId', 'Method', 'TenantId', 'ClientId', 'ManagedIdentityClientId', 'AccountId', 'AccountType', 'ARMToken', 'GraphToken', 'KeyVaultToken', 'LastError') {
                        if ($azureAuthSnapshot.PSObject.Properties.Name -contains $property) {
                            $ctx.$property = $azureAuthSnapshot.$property
                        }
                    }
                    if ($azureAuthSnapshot.PSObject.Properties.Name -contains 'SubscriptionIds') {
                        $ctx.SubscriptionIds = @($azureAuthSnapshot.SubscriptionIds)
                    }
                    if ($azureAuthSnapshot.PSObject.Properties.Name -contains 'TokenExpiresAt' -and $azureAuthSnapshot.TokenExpiresAt) {
                        $ctx.TokenExpiresAt = $azureAuthSnapshot.TokenExpiresAt
                    }
                    if ($azureAuthSnapshot.PSObject.Properties.Name -contains 'ConnectedAt' -and $azureAuthSnapshot.ConnectedAt) {
                        $ctx.ConnectedAt = $azureAuthSnapshot.ConnectedAt
                    }
                    $ctx.IsConnected = $true
                    $script:AzureAuthContext = $ctx
                    $script:AuthContext['Azure'] = $ctx
                }
                else {
                    $script:AzureAuthContext = $null
                }
            } $using:authSnapshot $using:azureAuthSnapshot

            $childBlock = [scriptblock]::Create($using:scriptBlockText)
            try {
                $result = @(& $childBlock $_)
                [pscustomobject]@{
                    Input   = $_
                    Success = $true
                    Result  = $result
                    Error   = $null
                }
            }
            catch {
                [pscustomobject]@{
                    Input   = $_
                    Success = $false
                    Result  = @()
                    Error   = $_.Exception.Message
                }
            }
        }
    }
}