I am queuing the Power BI (PBI) REST API to get the refresh history's for datasets on PBI. I'm then pushing the results back into a PBI Push Dataset for use in another report. The plan will then be to use a foreach loop to iterate the requests for every
dataset on PBI. This is the script so far;
# Parameters - fill these in before running the script!
# =====================================================
# An easy way to get group and dataset ID is to go to dataset settings and click on the dataset
# that you'd like to refresh. Once you do, the URL in the address bar will show the group ID and
# dataset ID, in the format:
# app.powerbi.com/groups/{groupID}/settings/datasets/{datasetID}
$groupID = "<REDACTED>" # the ID of the group that hosts the dataset. Use "me" if this is your My Workspace
$datasetID = "<REDACTED>" # the ID of the dataset that hosts the dataset
# AAD Client ID
# To get this, go to the following page and follow the steps to provision an app
# https://dev.powerbi.com/apps
# To get the sample to work, ensure that you have the following fields:
# App Type: Native app
# Redirect URL: urn:ietf:wg:oauth:2.0:oob
# Level of access: all dataset APIs
$clientId = "<REDACTED>"
# End Parameters =======================================
# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
function GetAuthToken
{
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
$authority = "https://login.microsoftonline.com/common/oauth2/authorize";
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
return $authResult
}
# Get the auth token from AAD
$token = GetAuthToken
# Building Rest API header with authorization token
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$token.CreateAuthorizationHeader()
}
# Properly format groups path
$groupsPath = ""
if ($groupID -eq "me") {
$groupsPath = "myorg"
} else {
$groupsPath = "myorg/groups/$groupID"
}
# Check the refresh history
$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"# + '?$top=1'
$refreshHistory = Invoke-RestMethod -Uri $uri –Headers $authHeader –Method GET –Verbose | Select-Object -ExpandProperty value | Select-Object id, refreshType, startTime, endTime, status
# Remove $refreshHistory in final
$refreshHistory
# Push data to Power BI
$endpoint = "https://api.powerbi.com/beta/<REDACTED>/datasets/<REDACTED>/rows?key=<REDACTED>"
$payload = $refreshHistory | ConvertTo-Json
# Remove $payload in final
$payload
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body $payload
This is an example of the $refreshHistory output;
id : 595702443
refreshType : Scheduled
startTime : 2019-03-31T03:00:11.91Z
endTime : 2019-03-31T03:00:45.883Z
status : Completed
id : 594772832
refreshType : Scheduled
startTime : 2019-03-30T03:02:03.353Z
endTime : 2019-03-30T03:02:22.937Z
status : Completed
And $payload;
[
{"id": 595702443,"refreshType": "Scheduled","startTime": "2019-04-16T02:00:05.583Z","endTime": "2019-03-31T03:00:45.883Z","status": "Completed"
},
{"id": 594772832,"refreshType": "Scheduled","startTime": "2019-03-30T03:02:03.353Z","endTime": "2019-03-30T03:02:22.937Z","status": "Completed"
}
]
What I want to do is add in the $datasetID so I can tie the refresh data back to a specific dataset when it comes to using the foreach loop. For example I would like my output for $refreshHistoryand $payload to be like;
id : 595702443
refreshType : Scheduled
startTime : 2019-03-31T03:00:11.91Z
endTime : 2019-03-31T03:00:45.883Z
status : CompleteddatasetId : dd3d8631-b7bb-4183-aa01-066852f201f5
id : 594772832
refreshType : Scheduled
startTime : 2019-03-30T03:02:03.353Z
endTime : 2019-03-30T03:02:22.937Z
status : Completed
datasetId : 793ec9d8-7efd-4341-a45f-d09217a2e210
[
{
"id": 595702443,"refreshType": "Scheduled","startTime": "2019-04-16T02:00:05.583Z","endTime": "2019-03-31T03:00:45.883Z","status": "Completed","datasetId": "dd3d8631-b7bb-4183-aa01-066852f201f5"
},
{"id": 594772832,"refreshType": "Scheduled","startTime": "2019-03-30T03:02:03.353Z","endTime": "2019-03-30T03:02:22.937Z","status": "Completed","datasetId": "793ec9d8-7efd-4341-a45f-d09217a2e210"
}
]
Is this possible?