Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Power BI Audit logs Extraction

Hi, Our team will be extractting the audit logs from Power BI Admin Portal Manually. Is there a way to automate, that extracts all the users who accessed the Reports on daily basis. can someone su...
  • mariussve1's avatar
    mariussve1
    4 years ago

    Hello Anonymous, 


    To fully automate this you should create an app user (Service principal). Here is a guide on how to do it:
    https://www.sqlbi.com/articles/creating-a-service-principal-account-for-power-bi-api

     

    When you have created a service principal you should have tenantid, appid and secret.
    Then you can use this script to take out yesterdays log:


    # Power BI Activity Events
    # Parameters
    $TenantId = "put yout tenantid here"
    $AppId = "put yout appid here" # Service PRincipal ID
    $Secret = "put yout secret here" # Secret from Service Principal

    # Connect the Service Principal
    $password = ConvertTo-SecureString $Secret -AsPlainText -Force
    $Creds = New-Object PSCredential $AppId, $password
    Connect-PowerBIServiceAccount -ServicePrincipal -Credential $Creds -Tenant $TenantId

    $headers = Get-PowerBIAccessToken

    1..1 |
    foreach {
    $Date = (((Get-Date).Date).AddDays(-$_))
    $StartDate = (Get-Date -Date ($Date) -Format yyyy-MM-ddTHH:mm:ss)
    $EndDate = (Get-Date -Date ((($Date).AddDays(1)).AddMilliseconds(-1)) -Format yyyy-MM-ddTHH:mm:ss)

    Get-PowerBIActivityEvent -StartDateTime $StartDate -EndDateTime $EndDate -ResultType JsonString |
    Out-File -FilePath "C:\Marius\PowerBI_AudititLog_$(Get-Date -Date $Date -Format yyyyMMdd).json"
    }

     

    If you want to take out the last 7 days, you can only change the script to:

     

    # Power BI Activity Events
    # Parameters
    $TenantId = "put yout tenantid here"
    $AppId = "put yout appid here" # Service PRincipal ID
    $Secret = "put yout secret here" # Secret from Service Principal

    # Connect the Service Principal
    $password = ConvertTo-SecureString $Secret -AsPlainText -Force
    $Creds = New-Object PSCredential $AppId, $password
    Connect-PowerBIServiceAccount -ServicePrincipal -Credential $Creds -Tenant $TenantId

    $headers = Get-PowerBIAccessToken

    1..7 |
    foreach {
    $Date = (((Get-Date).Date).AddDays(-$_))
    $StartDate = (Get-Date -Date ($Date) -Format yyyy-MM-ddTHH:mm:ss)
    $EndDate = (Get-Date -Date ((($Date).AddDays(1)).AddMilliseconds(-1)) -Format yyyy-MM-ddTHH:mm:ss)

    Get-PowerBIActivityEvent -StartDateTime $StartDate -EndDateTime $EndDate -ResultType JsonString |
    Out-File -FilePath "C:\Marius\PowerBI_AudititLog_$(Get-Date -Date $Date -Format yyyyMMdd).json"
    }

     

    I think the limit is 30 days.

     

    If you dont want to use service principal, you need to login every time you get the files. Then you have to use Connect-PowerBIServiceAccount like m-colbert mention in the earlier posts.

     

    Here you can read how to convert json to csv if this is important for you:

    https://www.techcartnow.com/powershell-script-to-convert-complex-nested-json-to-csv-file-format

     

    BR
    Marius