Functions/Get-BsgPbiActivity.ps1

<#
    .SYNOPSIS
        Get the json activity event files from PowerBI Tenant
         
    .DESCRIPTION
        Get the json activity event files from PowerBI Tenant
 
    .PARAMETER Path
        The path to the tenant folder.
        The folder needs to include the workspaces and mapping files.
 
    .EXAMPLE
        # Get all files from the last 30 days
        Get-BsgPbiActivity -Path "C:\temp\BSG PBI Administration"
         
    .INPUTS
 
    .OUTPUTS
     
    .NOTES
#>


function Get-BsgPbiActivity {
    param(
        [Parameter(Mandatory=$true)][string]$Path
    )

    
    
    ## Path Validation
    ##
    if ((Test-Path -Path $Path -PathType Container) -eq $false){
        throw "The path to save the json files doesn't exists or it isn't a folder."
    }



    ## Creating or Cleaning the ActivityEvents folder.
    ##
    $EventActivityPath = Join-Path -Path $Path -Child "ActivityEvents"
    if ((Test-Path -Path $EventActivityPath -PathType Container) -eq $false){
        $null = New-Item -Path $EventActivityPath -ItemType "directory"
        Write-Verbose ("Path " + $EventActivityPath + " has been created.")
    } else{
        $null = Remove-Item -Path ($EventActivityPath + "\*") -Recurse -ErrorAction Ignore
        Write-Verbose ("Path " + $EventActivityPath + " has been cleaned.")
    }

    ## Preparing the main variables
    ##
    $MainVerbose = $VerbosePreference
    $TotalDaysToDownload = 30
    $StartDate = (Get-Date).AddDays(-$TotalDaysToDownload)
    $EndDate = Get-Date
    $CurrentDay = 0
    
    

    ## Looping on the days
    ##
    while ($StartDate -lt $EndDate) {
        ## Prearing variables for the current day
        ##
        $DateFrom = $StartDate.ToString("yyyy-MM-dd") + "T00:00:00.000"
        $DateTo = $StartDate.ToString("yyyy-MM-dd") + "T23:59:59.999"
        $FileName = "ActivityEvent_" + $StartDate.ToString("yyyyMMdd") + ".json"
        $FilePath = Join-Path -Path $EventActivityPath -ChildPath $FileName

        ## Informing the progress with progresss bar or verbose message
        ##
        if ($MainVerbose -eq "SilentlyContinue"){
            $Progress = 100 * $CurrentDay / $TotalDaysToDownload
            $DateProgress = $StartDate.ToString("yyyy-MM-dd")
            Write-Progress -Activity "Downloading Event Activities" -Id 1 -Status $DateProgress -PercentComplete $Progress
        }
        Write-Verbose ("Downloading " + $FileName)
        
        ## Downloading the event file
        ##
        $VerbosePreference = "SilentlyContinue"
        $Response = Get-PowerBIActivityEvent -StartDateTime $DateFrom -EndDateTime $DateTo -ResultType JsonObject 
        $Response | Out-File -FilePath $FilePath
        $VerbosePreference = $MainVerbose

        
        ## Continue with the next day
        ##
        $StartDate = $StartDate.AddDays(1)
        $CurrentDay = $CurrentDay + 1
    }

    ## Clossing the progress bar
    Write-Progress -Activity "Downloading Event Activities" -Id 1 -Completed
  
}