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. .PARAMETER DaysBack Number of days of logging to get. It should be as maximum 30 days. By Default it will download the last day .EXAMPLE # Get all files from the last day Get-BsgPbiActivity -Path "C:\temp\BSGPBIAdministration" .EXAMPLE # Get all files from the last 30 day Get-BsgPbiActivity -Path "C:\temp\BSGPBIAdministration" -DaysBack 30 .INPUTS .OUTPUTS .NOTES #> function Get-BsgPbiActivity { param( [Parameter(Mandatory=$true)][string]$Path, [Parameter(Mandatory=$false)][int]$DaysBack = 1 ) ## 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.") } ## Preparing the main variables ## $MainVerbose = $VerbosePreference if ($DaysBack -gt 30 -or $DaysBack -le 0) { throw "Wrong DayBack value. It should be between 1 and 30" } $TotalDaysToDownload = $DaysBack $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 $URL = "https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime='" + $DateFrom + "'&endDateTime='" + $DateTo + "'" ## 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 ## try{ $FinalJson = $null $VerbosePreference = "SilentlyContinue" $ContinuationURL = $null do { ## FIRST request ## if ($null -eq $ContinuationURL){ $Response = Invoke-PowerBIRestMethod -Url $URL -Method GET | ConvertFrom-Json $ContinuationURL = $Response.continuationUri $FinalJson = $Response.activityEventEntities } ## CONTINUATION request ## else{ $Response = Invoke-PowerBIRestMethod -Url $ContinuationURL -Method GET | ConvertFrom-Json $ContinuationURL = $Response.continuationUri $FinalJson += $Response.activityEventEntities } } while ($ContinuationURL) $VerbosePreference = $MainVerbose if ($FinalJson.Count -gt 0) { $FinalJson | ConvertTo-Json | Out-File -FilePath $FilePath } }catch { Write-Error $_.Exception } ## 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 } |