REST API 使用 PowerShell 分别部署报告或仪表板。

huangapple go评论87阅读模式
英文:

REST API deploys repot or Dashboard separately with powershell

问题

我们知道,我们可以使用部署管道将报告或仪表板从开发环境部署到下一个环境,如SIT。

现在我想使用PowerShell来调用REST API来完成这个任务,但我对Power BI的REST API不太熟悉,有什么建议吗?

不要只提供官方API链接,提供一个具体的示例会更好。

英文:

As we know, we could use the deployment pipeline only deploy the report or Dashboard from Dev environment to the next environment, like SIT.

Now I want to use the powershell invoke the REST API to complete this, but I am not familiar with power bi's REST API, any suggestion?

Please don't just provide an official API link, a specific sample would be better.

答案1

得分: 1

以下是您要翻译的内容:

一个具体的例子会更好

您会在 microsoft/PowerBI-Developer-Samples PowerShell Scripts 中找到 很多 使用 Power BI REST APIs 的示例。

例如,rebindReport.ps1,带有以下说明:

  1. 安装
  2. 以管理员身份运行 PowerShell
  3. 按照下面的说明填写客户端 ID
  4. 将 PowerShell 目录更改为保存此脚本的位置
  5. ./rebindReport.ps1
# 此示例脚本调用 Power BI API,以编程方式将源报表克隆到 Power BI 服务中的目标报表。克隆可以基于相同的数据集或新数据集

# 有关文档,请参阅:
# https://msdn.microsoft.com/en-us/library/mt784674.aspx

# 说明:
# 1. 安装 PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell)
#    以及 Azure PowerShell cmdlets (Install-Module AzureRM)
# 2. 以管理员身份运行 PowerShell
# 3. 按照下面的说明填写客户端 ID
# 4. 将 PowerShell 目录更改为保存此脚本的位置
# 5. > ./rebindReport.ps1

# 参数 - 在运行脚本之前填写这些参数!
# =====================================================

# 源报表信息
# 获取此信息的简便方法是导航到 Power BI 服务中的报表
# URL 将包含以下格式的组和报表 ID:
# app.powerbi.com/groups/{groupID}/report/{reportID}

$sourceReportGroupId = "请填写"    # 托管源报表的组(工作区)的 ID。如果这是您的 My Workspace,则使用 "me"
$sourceReportId = "请填写"         # 源报表的 ID

# 目标报表信息
# 获取组和数据集 ID 的简便方法是转到数据集设置并单击要刷新的数据集。
# 一旦这样做,地址栏中的 URL 将显示组 ID 和数据集 ID,格式如下:
# app.powerbi.com/groups/{groupID}/settings/datasets/{datasetID}

$targetReportName = "请填写"       # 您想要为目标报表命名的内容
$targetGroupId = "请填写"          # 您想要将报表移动到的组(工作区)的 ID。如果要克隆到相同的工作区,请留空。如果这是您的 My Workspace,请使用 "me"
$targetDatasetId = "请填写"        # 您想要将目标报表重新绑定到的数据集的 ID。如果要使用相同的数据集,请留空

# AAD 客户端 ID
# 要获取此 ID,请转到以下页面并按照步骤设置应用程序
# https://dev.powerbi.com/apps
# 为了使示例正常工作,请确保具有以下字段:
# 应用程序类型:本机应用程序
# 重定向 URL:urn:ietf:wg:oauth:2.0:oob
# 访问级别:所有数据集 API
$clientId = "请填写"

# 结束参数 =======================================

# 调用 Active Directory Authentication Library (ADAL) 进行 AAD 认证
function GetAuthToken
{

    $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
}

# 从 AAD 获取身份验证令牌
$token = GetAuthToken

# 使用授权令牌构建 Rest API 标头
$authHeader = @{
   'Content-Type'='application/json'
   'Authorization'=$token.CreateAuthorizationHeader()
}

# 正确格式化组路径
$sourceGroupsPath = ""
if ($sourceReportGroupId -eq "me") {
    $sourceGroupsPath = "myorg"
} else {
    $sourceGroupsPath = "myorg/groups/$sourceReportGroupId"
}

# POST 请求体
$postParams = @{
    "Name" = "$targetReportName"
    "TargetWorkspaceId" = "$targetGroupId"
    "TargetModelId" = "$targetDatasetId"
}

$jsonPostBody = $postParams | ConvertTo-JSON

# 发出克隆报表的请求
$uri = "https://api.powerbi.com/v1.0/$sourceGroupsPath/reports/$sourceReportId/clone"
Invoke-RestMethod -Uri $uri Headers $authHeader Method POST -Body $jsonPostBody Verbose
英文:

> A specific example would be better

You would find a lot of examples in microsoft/PowerBI-Developer-Samples PowerShell Scripts, which are using the Power BI REST APIs

For instance, rebindReport.ps1, with the instructions:

> 1. Install
> - PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell)
> - and the Azure PowerShell cmdlets (Install-Module AzureRM)
> 2. Run PowerShell as an administrator
> 3. Follow the instructions below to fill in the client ID
> 4. Change PowerShell directory to where this script is saved
> 5. ./rebindReport.ps1

# This sample script calls the Power BI API to programmatically clone a SOURCE report to a 
# TARGET report in the Power BI service. The clone can either be based off of the same 
# dataset or a new dataset

# For documentation, please see:
# https://msdn.microsoft.com/en-us/library/mt784674.aspx

# Instructions:
# 1. Install PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell) 
#    and the Azure PowerShell cmdlets (Install-Module AzureRM)
# 2. Run PowerShell as an administrator
# 3. Follow the instructions below to fill in the client ID
# 4. Change PowerShell directory to where this script is saved
# 5. > ./rebindReport.ps1

# Parameters - fill these in before running the script!
# =====================================================

# SOURCE report info
# An easy way to get this is to navigate to the report in the Power BI service
# The URL will contain the group and report IDs with the following format:
# app.powerbi.com/groups/{groupID}/report/{reportID} 

$sourceReportGroupId = " FILL ME IN "    # the ID of the group (workspace) that hosts the source report. Use "me" if this is your My Workspace
$sourceReportId = " FILL ME IN "         # the ID of the source report

# TARGET report info
# 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} 

$targetReportName = " FILL ME IN "       # what you'd like to name the target report
$targetGroupId = " FILL ME IN "          # the ID of the group (workspace) that you'd like to move the report to. Leave this blank if you'd like to clone to the same workspace. Use "me" if this is your My Workspace
$targetDatasetId = " FILL ME IN "        # the ID of the dataset that you'd like to rebind the target report to. Leave this blank to have the target report use the same 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 = " FILL ME IN " 

# End Parameters =======================================

# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
function GetAuthToken
{

    $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
$sourceGroupsPath = ""
if ($sourceReportGroupId -eq "me") {
    $sourceGroupsPath = "myorg"
} else {
    $sourceGroupsPath = "myorg/groups/$sourceReportGroupId"
}

# POST body 
$postParams = @{
    "Name" = "$targetReportName"
    "TargetWorkspaceId" = "$targetGroupId"
    "TargetModelId" = "$targetDatasetId"
}

$jsonPostBody = $postParams | ConvertTo-JSON

# Make the request to clone the report
$uri = "https://api.powerbi.com/v1.0/$sourceGroupsPath/reports/$sourceReportId/clone"
Invoke-RestMethod -Uri $uri Headers $authHeader Method POST -Body $jsonPostBody Verbose

huangapple
  • 本文由 发表于 2023年7月18日 10:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定