modules/Azure/Discovery/Public/Save-CIEMAzureEntraResource.ps1

function Save-CIEMAzureEntraResource {
    [CmdletBinding(DefaultParameterSetName = 'ByProperties')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Upsert operation for bulk data')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ByProperties')]
        [string]$Id,

        [Parameter(Mandatory, ParameterSetName = 'ByProperties')]
        [string]$Type,

        [Parameter(ParameterSetName = 'ByProperties')]
        [string]$DisplayName,

        [Parameter(ParameterSetName = 'ByProperties')]
        [string]$ParentId,

        [Parameter(ParameterSetName = 'ByProperties')]
        [string]$Properties,

        [Parameter(ParameterSetName = 'ByProperties')]
        [string]$CollectedAt = (Get-Date).ToString('o'),

        [Parameter(ParameterSetName = 'ByProperties')]
        [long]$LastSeenAt = 0,

        [Parameter(Mandatory, ParameterSetName = 'InputObject', ValueFromPipeline)]
        [PSObject[]]$InputObject,

        [Parameter()]
        [Microsoft.Data.Sqlite.SqliteConnection]$Connection
    )

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

    process {
        if ($PSCmdlet.ParameterSetName -eq 'InputObject') {
            foreach ($item in $InputObject) {
                $items.Add([pscustomobject]@{
                    Id = $item.Id
                    Type = $item.Type
                    DisplayName = $item.DisplayName
                    ParentId = $item.ParentId
                    Properties = $item.Properties
                    CollectedAt = if ($item.CollectedAt) { $item.CollectedAt } else { (Get-Date).ToString('o') }
                    LastSeenAt = if ($item.PSObject.Properties.Name -contains 'LastSeenAt') { [long]$item.LastSeenAt } else { 0 }
                })
            }
            return
        }

        $items.Add([pscustomobject]@{
            Id = $Id
            Type = $Type
            DisplayName = $DisplayName
            ParentId = $ParentId
            Properties = $Properties
            CollectedAt = $CollectedAt
            LastSeenAt = $LastSeenAt
        })
    }

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

        $columns = @('id', 'type', 'display_name', 'parent_id', 'properties', 'collected_at', 'last_seen_at')

        for ($offset = 0; $offset -lt $items.Count; $offset += $script:CIEMSqlBatchSize) {
            $remaining = $items.Count - $offset
            $chunkSize = [Math]::Min($script:CIEMSqlBatchSize, $remaining)
            $rows = @()
            $parameters = @{}

            for ($rowIndex = 0; $rowIndex -lt $chunkSize; $rowIndex++) {
                $item = $items[$offset + $rowIndex]
                $suffix = $rowIndex + 1
                $rows += "(@id_$suffix, @type_$suffix, @display_name_$suffix, @parent_id_$suffix, @properties_$suffix, @collected_at_$suffix, @last_seen_at_$suffix)"

                $parameters["id_$suffix"] = $item.Id
                $parameters["type_$suffix"] = $item.Type
                $parameters["display_name_$suffix"] = $item.DisplayName
                $parameters["parent_id_$suffix"] = $item.ParentId
                $parameters["properties_$suffix"] = $item.Properties
                $parameters["collected_at_$suffix"] = $item.CollectedAt
                $parameters["last_seen_at_$suffix"] = $item.LastSeenAt
            }

            $sql = "INSERT OR REPLACE INTO azure_entra_resources ($($columns -join ', ')) VALUES $($rows -join ', ')"

            if ($Connection) {
                Invoke-PSUSQLiteQuery -Connection $Connection -Query $sql -Parameters $parameters -AsNonQuery | Out-Null
            }
            else {
                Invoke-CIEMQuery -Query $sql -Parameters $parameters -AsNonQuery | Out-Null
            }
        }
    }
}