<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Push data into dataset using PowerShell in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Push-data-into-dataset-using-PowerShell/m-p/669965#M19122</link>
    <description>&lt;P&gt;I have successfully connected to Power BI via PowerShell and I can refresh a dataset using the very helpful information here&amp;nbsp;&lt;A href="https://www.fourmoo.com/2018/06/05/using-the-power-bi-api-with-powershell-scripts-refreshing-your-dataset/" target="_blank" rel="noopener"&gt;https://www.fourmoo.com/2018/06/05/using-the-power-bi-api-with-powershell-scripts-refreshing-your-dataset/&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to be able to do now is to build a report that pulls the refresh history of each dataset and then pushes that data to a Push dataset so end users can view the status of all reports and if the refresh is in progress the estimated time it will complete.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The PowerShell script to get the refresh history is;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# Parameters - fill these in before running the script!
# =====================================================

$groupID = "REDACTED" # the ID of the group that hosts the dataset. Use "me" if this is your My Workspace
$datasetID = "REDACTED"
$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&lt;BR /&gt;# Uncomment + '?$top=1' to get the most recent refresh
$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
# Remove $refreshHistory in final
$refreshHistory&lt;/PRE&gt;&lt;P&gt;This gives the complete refresh&amp;nbsp;history for my dataset, an example is below;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 325px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/158891iC07879BCDF4673FE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next step is to push that data back to Power BI. I've got the PowerShell that I need to use from the &lt;STRONG&gt;API info&lt;/STRONG&gt; page, as below with parts redacted.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Untitled.png" style="width: 999px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/158893i95E0712842271850/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So all that is need is to replace the examples in the payload section with what I get from my previous request, an example with just the &lt;STRONG&gt;id&lt;/STRONG&gt;&amp;nbsp;using the response data is below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;$endpoint = "https://api.powerbi.com/beta/&amp;lt;REDACTED&amp;gt;/datasets/&amp;lt;REDACTED&amp;gt;/rows?key=&amp;lt;REDACTED&amp;gt;"
$payload = @{
"id" = $refreshHistory | Select-Object -ExpandProperty id
"refreshType" ="TEST"
"startTime" ="2019-04-15T00:00:00.000Z"
"endTime" ="2019-04-15T01:00:00.000Z"
"status" ="TEST"
}
# Remove $payload in final
$payload
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))&lt;/PRE&gt;&lt;P&gt;The above works if I were just to get the most recent refresh, i.e. one result. But I get an error when trying to post the whole refresh history i.e. multiple results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled2.png" style="width: 953px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/158895i90F8C192D54B73ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled2.png" alt="Untitled2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I assume this is because the script is attempting&amp;nbsp;to push an array rather than a single result. How can modify my PowerShell to push the whole refresh history?&lt;/P&gt;</description>
    <pubDate>Mon, 15 Apr 2019 10:42:55 GMT</pubDate>
    <dc:creator>mark_carlisle</dc:creator>
    <dc:date>2019-04-15T10:42:55Z</dc:date>
    <item>
      <title>Push data into dataset using PowerShell</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Push-data-into-dataset-using-PowerShell/m-p/669965#M19122</link>
      <description>&lt;P&gt;I have successfully connected to Power BI via PowerShell and I can refresh a dataset using the very helpful information here&amp;nbsp;&lt;A href="https://www.fourmoo.com/2018/06/05/using-the-power-bi-api-with-powershell-scripts-refreshing-your-dataset/" target="_blank" rel="noopener"&gt;https://www.fourmoo.com/2018/06/05/using-the-power-bi-api-with-powershell-scripts-refreshing-your-dataset/&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to be able to do now is to build a report that pulls the refresh history of each dataset and then pushes that data to a Push dataset so end users can view the status of all reports and if the refresh is in progress the estimated time it will complete.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The PowerShell script to get the refresh history is;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# Parameters - fill these in before running the script!
# =====================================================

$groupID = "REDACTED" # the ID of the group that hosts the dataset. Use "me" if this is your My Workspace
$datasetID = "REDACTED"
$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&lt;BR /&gt;# Uncomment + '?$top=1' to get the most recent refresh
$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
# Remove $refreshHistory in final
$refreshHistory&lt;/PRE&gt;&lt;P&gt;This gives the complete refresh&amp;nbsp;history for my dataset, an example is below;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 325px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/158891iC07879BCDF4673FE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next step is to push that data back to Power BI. I've got the PowerShell that I need to use from the &lt;STRONG&gt;API info&lt;/STRONG&gt; page, as below with parts redacted.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Untitled.png" style="width: 999px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/158893i95E0712842271850/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So all that is need is to replace the examples in the payload section with what I get from my previous request, an example with just the &lt;STRONG&gt;id&lt;/STRONG&gt;&amp;nbsp;using the response data is below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;$endpoint = "https://api.powerbi.com/beta/&amp;lt;REDACTED&amp;gt;/datasets/&amp;lt;REDACTED&amp;gt;/rows?key=&amp;lt;REDACTED&amp;gt;"
$payload = @{
"id" = $refreshHistory | Select-Object -ExpandProperty id
"refreshType" ="TEST"
"startTime" ="2019-04-15T00:00:00.000Z"
"endTime" ="2019-04-15T01:00:00.000Z"
"status" ="TEST"
}
# Remove $payload in final
$payload
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))&lt;/PRE&gt;&lt;P&gt;The above works if I were just to get the most recent refresh, i.e. one result. But I get an error when trying to post the whole refresh history i.e. multiple results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled2.png" style="width: 953px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/158895i90F8C192D54B73ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled2.png" alt="Untitled2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I assume this is because the script is attempting&amp;nbsp;to push an array rather than a single result. How can modify my PowerShell to push the whole refresh history?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 10:42:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Push-data-into-dataset-using-PowerShell/m-p/669965#M19122</guid>
      <dc:creator>mark_carlisle</dc:creator>
      <dc:date>2019-04-15T10:42:55Z</dc:date>
    </item>
    <item>
      <title>Re: Push data into dataset using PowerShell</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Push-data-into-dataset-using-PowerShell/m-p/3176448#M42096</link>
      <description>&lt;P&gt;You will have to use the for each loop to traverse thru each of the refreshes for the given dataset... something like below... i have tried and it works&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;foreach($row in $refreshHistory)&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;$endpoint = "&lt;A href="https://api.powerbi.com/beta/" target="_blank"&gt;https://api.powerbi.com/beta/&lt;/A&gt;&amp;lt;REDACTED&amp;gt;/datasets/&amp;lt;REDACTED&amp;gt;/rows?key=&amp;lt;REDACTED&amp;gt;"&lt;BR /&gt;$payload = @{&lt;BR /&gt;"requestId" =$row.requestId&lt;BR /&gt;"id" =$row.id&lt;BR /&gt;"refreshType" =$row.refreshType&lt;BR /&gt;"startTime" =$row.startTime&lt;BR /&gt;"endTime" =$row.endTime&lt;BR /&gt;"status" =$row.status&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 17:07:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Push-data-into-dataset-using-PowerShell/m-p/3176448#M42096</guid>
      <dc:creator>rmkmurthy</dc:creator>
      <dc:date>2023-04-06T17:07:27Z</dc:date>
    </item>
  </channel>
</rss>

