Public/TenantConfiguration/Enable-Auditlogging.ps1

function Enable-AuditLogging {
    param (
        [Parameter(Mandatory)]
        [string]$TenantId
    )

    Write-Host "[AuditLogging] Starting configuration..." -ForegroundColor Yellow

    try {
        Connect-CustomerExchange -CustomerTenantId $TenantId
    }
    catch {
        throw "Could not connect to Exchange : $_"
    }

    $Customization = Get-OrganizationConfig | Select-Object -ExpandProperty IsDehydrated

    if ( $Customization -ne $false) {
        Write-Host "[AuditLogging] Customization is Disabled. Trying to enable" -ForegroundColor Cyan
        Enable-OrganizationCustomization
    }

    else {
        Write-Host "[AuditLogging] Customization is already enabled..." -ForegroundColor Yellow
    }

    $AuditloggingEnabled = Get-AdminAuditLogConfig | Select-Object UnifiedAuditLogIngestionEnabled -ExpandProperty UnifiedAuditLogIngestionEnabled
    if ( $AuditloggingEnabled -eq $True) {
        Write-Host "[AuditLogging] Auditlogging is already enabled..." -ForegroundColor Yellow
    }

    else {
        try {
            Set-AdminAuditLogConfig -UnifiedAuditLogIngestionEnabled $true

            $AuditloggingEnabled = Get-AdminAuditLogConfig | Select-Object UnifiedAuditLogIngestionEnabled -ExpandProperty UnifiedAuditLogIngestionEnabled
            
            if( $AuditloggingEnabled -ne $true ) {
                throw "Auditlogging is not enabled.."
            }

            else {
                Write-Host "[AuditLogging] Auditlogging enabled." -ForegroundColor Green
            }
        }
        catch {
            throw "Could not enable auditlogging : $_"
        }
    }
}