Public/Configuration/ExchangeOnline/Enable-Auditlogging.ps1

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

    Write-ModuleLog -Message "Starting configuration..." -Level Info -Component 'AuditLogging'

    try {
        Connect-CustomerExchange -CustomerTenantId $TenantId
    }
    catch {
        Write-ModuleLog -Message "Could not connect to Exchange" -Level Error -Component 'AuditLogging' -ErrorRecord $_ -ThrowError
    }

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

    if ( $Customization -ne $false) {
        Write-ModuleLog -Message "Organization Customization is disabled, enabling." -Level Info -Component 'AuditLogging'
        Enable-OrganizationCustomization
    }

    else {
        Write-ModuleLog -Message "Organization Customization is already enabled." -Level Info -Component 'AuditLogging'
    }

    $AuditloggingEnabled = Get-AdminAuditLogConfig | Select-Object UnifiedAuditLogIngestionEnabled -ExpandProperty UnifiedAuditLogIngestionEnabled
    if ( $AuditloggingEnabled -eq $True) {
        Write-ModuleLog -Message "Audit logging is already enabled." -Level Info -Component 'AuditLogging'
    }

    else {
        try {
            Set-AdminAuditLogConfig -UnifiedAuditLogIngestionEnabled $true

            $AuditloggingEnabled = Get-AdminAuditLogConfig | Select-Object UnifiedAuditLogIngestionEnabled -ExpandProperty UnifiedAuditLogIngestionEnabled
            
            if( $AuditloggingEnabled -ne $true ) {
                Write-ModuleLog -Message "Could not enable auditlogging." -Level Error -Component 'AuditLogging' -ThrowError
            }

            else {
                Write-moduleLog -Message "Auditlogging enabled." -Level Info -Component 'AuditLogging'
            }
        }
        catch {
            Write-ModuleLog "Could not enable auditlogging : $_" -Level Error -Component 'AuditLogging' -ErrorRecord $_ -ThrowError 
        }
    }
}