Public/Start-IAMCoreConnectorSyncJob.ps1

function Start-IAMCoreConnectorSyncJob {
    [CmdletBinding()]

    Param(
        [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true)]
        [Alias("ConnectorId")]
        [ValidatePattern("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$")]
        [string]$Id,

        [Parameter(Mandatory = $false)]
        [switch]$Wait,

        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 60)]
        [int]$SleepSeconds = 10,

        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 2)]
        [int]$ParallellCount = 2
    )

    Begin {
        if (!$Script:AccessTokenProfile) {
            throw "Access token profile is not set. Please connecting using Connect-IAMCore before starting a sync job."
        }

        # If the Wait parameter is set, print a warning that it is deprecated
        if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Wait')) {
            Write-Warning "The -Wait parameter is deprecated and will be removed in future versions."
        }

        Write-Verbose "Starting the IAM Core connector sync job with parallel count $ParallellCount"
        $Script:ConnectorIds = @{}
    }

    Process {
        if (!$Id) { return; }
        $Script:ConnectorIds[$Id] = $null
    }

    End {
        $FinishedStates = "Completed", "Failed", "TimedOut", "WhatIfSkipped"
        $FailedStates = "Failed", "TimedOut"

        $AllFinished = $false
        while (!$AllFinished) {
            # Update the status of all running sync jobs
            $Keys = $Script:ConnectorIds.Keys | ForEach-Object { $_ }
            $Keys | ForEach-Object {
                $SyncJob = $Script:ConnectorIds[$_]
                if ($null -ne $SyncJob -and $SyncJob.State -notin $FinishedStates) {
                    Write-Debug "Checking status of sync job $($SyncJob.Id)"
                    $PreviousValue = $Script:ConnectorIds[$_]
                    $Script:ConnectorIds[$_] = Get-IAMCoreSyncJob -Id $SyncJob.Id -Verbose:$false

                    if ($Script:ConnectorIds[$_].State -in $FailedStates) {
                        Write-Warning "Sync job $($SyncJob.Id) has failed with state $($Script:ConnectorIds[$_].State) for connector $($_)"
                    }
                    elseif ($Script:ConnectorIds[$_].State -ne $PreviousValue.State) {
                        Write-Verbose "Sync job $($SyncJob.Id) for connector $($_) went from $($PreviousValue.State) to $($Script:ConnectorIds[$_].State)"
                    }
                }
            }

            # Calculate the number of running and unstarted sync jobs
            $RunningJobsCount = $Script:ConnectorIds.Values | Where-Object { $_ -and $_.State -notin $FinishedStates } | Measure-Object | Select-Object -ExpandProperty Count
            $UnstartedSyncJobs = $Script:ConnectorIds.Keys | Where-Object { $null -eq $Script:ConnectorIds[$_] }

            # Start new sync jobs for connectors that haven't started yet, up to the parallel count limit
            $UnstartedSyncJobs | 
            Select-Object -First ([Int]::Max($ParallellCount - $RunningJobsCount, 0)) |
            ForEach-Object {
                Write-Verbose "Starting sync job for connector with ID $_"
                $Script:ConnectorIds[$_] = New-IAMCoreSyncJob -ConnectorId $_ -JobType ConnectorImport -Verbose:$false
            }

            # Update the flag indicating whether all sync jobs have finished
            $AllFinished = -not ($Script:ConnectorIds.Keys | Where-Object { !$Script:ConnectorIds[$_] -or $Script:ConnectorIds[$_].State -notin $FinishedStates })
            $RunningJobsCount = $Script:ConnectorIds.Values | Where-Object { $_ -and $_.State -notin $FinishedStates } | Measure-Object | Select-Object -ExpandProperty Count
            $FinishedJobsCount = $Script:ConnectorIds.Values | Where-Object { $_ -and $_.State -in $FinishedStates } | Measure-Object | Select-Object -ExpandProperty Count
            $UnstartedSyncJobs = $Script:ConnectorIds.Keys | Where-Object { $null -eq $Script:ConnectorIds[$_] }

            if (!$AllFinished) {
                Write-Verbose "Waiting for sync jobs to complete. Running jobs count: $RunningJobsCount, unstarted jobs count: $($UnstartedSyncJobs.Count), finished jobs count: $FinishedJobsCount"
                Start-Sleep -Seconds $SleepSeconds
            }
        }
    }
}