Skip to content

Instantly share code, notes, and snippets.

@zachrenwick
Created June 17, 2022 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachrenwick/f783407cf65c1f5930b659079004d365 to your computer and use it in GitHub Desktop.
Save zachrenwick/f783407cf65c1f5930b659079004d365 to your computer and use it in GitHub Desktop.
Power BI Logs for Last 30 Days
cls
#don't forget to install the shell module
#Install-Module MicrosoftPowerBIMgmt.Admin
#Install-Module MicrosoftPowerBIMgmt.Capacities
#Install-Module MicrosoftPowerBIMgmt.Data
#Install-Module MicrosoftPowerBIMgmt.Profile
#Install-Module MicrosoftPowerBIMgmt.Reports
#Install-Module MicrosoftPowerBIMgmt.Workspaces
Connect-PowerBIServiceAccount
Write-Host("Step 1 : Login....Done")
#Loading Parametres
#Date Load
$RetrieveDate = (Get-Date).AddDays(-30)
$BasePath = "C:\PBI Logs\Data\"
$time = date($RetrieveDate) -Format "yyyyMMdd"
$Filename = date($RetrieveDate) -Format "yyyyMMdd"
#Export Paths
$Groupspath = $BasePath + $Filename+"_Workspaces.json"
$ActivityLogsPath = $BasePath + $Filename+"_Activities.csv"
$CapacitiesPath = $BasePath + $Filename+"_Capacities.csv"
$RefreshablesPath = $BasePath + $Filename+"_Refreshables.csv"
$GatewaysPath = $BasePath + $Filename+"_DataGateway.csv"
$DatasourcesPath = $BasePath + $Filename+"_Datasources.json"
Write-Host("Step 2 : Loading Activities....")
$RetrieveYearStr = $RetrieveDate.ToString('yyyy')
$RetrieveMonthStr = $RetrieveDate.ToString('MM')
$RetrieveDayStr = $RetrieveDate.ToString('dd')
# set Startdate & Enddate
$StartDT = $RetrieveYearStr + '-' + $RetrieveMonthStr + '-' + $RetrieveDayStr + 'T00:00:00'
$EndDT = $RetrieveYearStr + '-' + $RetrieveMonthStr + '-' + $RetrieveDayStr + 'T23:59:59'
$ActivityType = 'ViewReport'
# Function to retrive power bi activities
$ActivityLogs = Get-PowerBIActivityEvent -StartDateTime $StartDt -EndDateTime $EndDT -ActivityType $ActivityType | ConvertFrom-Json
# retrive activities datas
$ActivityLogSchema = $ActivityLogs | `
Select-Object Id,RecordType,CreationTime,Operation,OrganizationId,UserType,UserKey,Workload, `
UserId,ClientIP,UserAgent,Activity,ItemName,WorkspaceName,DatasetName,ReportName, `
WorkspaceId,CapacityId,CapacityName,DataflowId,DataflowName,DashboardName,AppName,ObjectId,DatasetId,ReportId,IsSuccess, `
ReportType,RequestId,ActivityId,AppReportId,DistributionMethod,ConsumptionMethod, `
@{Name="RetrieveDate";Expression={$RetrieveDate}}
# Exporting Activities Data
$ActivityLogSchema | Export-Csv $ActivityLogsPath
Write-Host("Step 2 : Loading Activities....Done")
################## END ###########################################
################## GET WORKSPACES DATA###########################################
Write-Host("Step 3 : Loading Groups....")
$GroupsURI = '/admin/groups?$top=5000&' + '$filter=type ne' + " 'PersonalGroup'" + ' and state eq' + " 'Active'" + '&$expand=users,reports,dashboards,datasets,dataflows'
$GroupsData = Invoke-PowerBIRestMethod -Url $GroupsURI -Method Get | ConvertFrom-Json
# getting datasetid to be used in the datasource API
$DatasetId = $GroupsData.value.datasets.id
$GroupsData | ConvertTo-Json -Depth 20 | out-File $Groupspath
Write-Host("Step 3 : Loading Groups....Done")
################## END ###########################################
################## GET CAPACITIES DATA###########################################
Write-Host("Step 4 : Loading Capacities.....")
$CapacitiesURI = '/admin/capacities'
Invoke-PowerBIRestMethod -Url $CapacitiesURI -Method Get | Out-File $CapacitiesPath
Write-Host("Step 4 : Loading Capacities.....Done")
################## END ###########################################
################## GET Refreshables DATA###########################################
Write-Host("Step 5 : Loading Refreshables.....")
$RefreshablesURI = 'admin/capacities/refreshables?$expand=capacity,group'
Invoke-PowerBIRestMethod -Url $RefreshablesURI -Method Get | Out-File $RefreshablesPath
Write-Host("Step 5 : Loading Refreshables.....Done")
################## END ###########################################
################## GET Gateways DATA###########################################
Write-Host("Step 6: Loading Gateways.....")
$GatewaysURI = 'https://api.powerbi.com/v2.0/myorg/gatewayClusters?$expand=memberGateways'
Invoke-PowerBIRestMethod -Url $GatewaysURI -Method Get | Out-File $GatewaysPath
Write-Host("Step 6: Loading Gateways.....Done")
#Disconnect
Disconnect-PowerBIServiceAccount
@zachrenwick
Copy link
Author

#ToDo
-- Currently not working for more than 1 day at a time! Warning!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment