We have the following Power Shell script, to get the logs for SharePoint sites:-

$OutputFile = ".\UnifiedAuditLog_FULL.csv"
$Today = Get-Date -Date (Get-Date -Format “yyyy-MM-dd”)
$intDays = 14
For ($i=0; $i -le $intDays; $i++){
  For ($j=23; $j -ge 0; $j--){
    $StartDate = ($Today.AddDays(-$i)).AddHours($j)
    $EndDate = ($Today.AddDays(-$i)).AddHours($j + 1)
    $Audit = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -ResultSize 5000
    $ConvertAudit = $Audit | Select-Object -ExpandProperty AuditData | ConvertFrom-Json
    $ConvertAudit | Select-Object CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent | Export-Csv $OutputFile -NoTypeInformation -Append
    Write-Host $StartDate `t $Audit.Count

now we can manually copy/paste the script inside Windows Power Shell windows >> login to our tenant for MFA >> and run the script.

but my question, is how we can automate the execution of this Power Shell to run daily on the background? in other words how we can login to ExchnageOnline without human interaction?


2 Answers 2


You can use app-only authentication for unattended Exchange Online PowerShell scripts. You'll need to setup an app registration and a certificate to authenticate. More information can be found here:

App-only authentication for unattended scripts in Exchange Online PowerShell and Security & Compliance PowerShell


Here is PowerShell script I use: Search Unified Audit Log Daemon Job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.