Power BI Embedded を PowerShell で操作する

鈴木です。

MODD Web Service にリッチ な対話型レポーティングサービスを導入するため、
Microsoft Azure の Power BI Embedded を検証してみました。

Microsoft Power BI Embedded とは

docs.microsoft.com
本ブログとしては、以下の記事を可能な限りPowerShellで行っています。
REST 経由で Power BI Embedded を使用する方法 | Microsoft Docs


事前準備

Microsoft Power BI Embedded - データ ソースへの接続 | Microsoft Docs
Power BI Embedded のデータソースとなるSQL データベース の作成は事前に済ませておきました。Power BI Embedded は、DirectQueryで利用する場合、サポートされるデータソースが「Azure SQL データベース」と「Azure SQL Data Warehouse」だけと少なめです。

PowerShell による操作

AzureRM のインストール

Install and configure Azure PowerShell | Microsoft Docs
PowerShell からAzureを操作するために、AzureRMをインストールします。…が、Power BI Embedded に関連するコマンドは数えるほどしか実装されていませんので、手順のいくつかはREST 経由で利用することになります。

Install-Module AzureRM
Import-Module AzureRM

ログイン情報の設定

#Set Credential
$User       = "dummy@crosswarp.com"
$PassPlain  = "XXXXXXXX"
$Pass       = ConvertTo-SecureString -AsPlainText -Force -String $PassPlain
$Credential = New-Object System.Management.Automation.PSCredential $User, $Pass

#Login Azure
Login-AzureRmAccount -Credential $Credential
#Check
Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionName "サブスクリプション名"

最後にリソースグループが取得できるか確認します。

#Get ResourceGroup
$ResourceGroup     = Get-AzureRmResourceGroup
$ResourceGroupName = $ResourceGroup[0].ResourceGroupName

Power BI ワークスペースコレクションを作成する

#New PowerBI WorkspaceCollection
$WorkspaceCollectionName = "Pbi-MyWorkpace"
$Location                = "Japan East"
#https://docs.microsoft.com/en-us/powershell/module/azurerm.powerbiembedded/new-azurermpowerbiworkspacecollection?view=azurermps-4.1.0
New-AzureRmPowerBIWorkspaceCollection -ResourceGroupName $ResourceGroupName `
                                      -WorkspaceCollectionName $WorkspaceCollectionName `
                                      -Location $Location

$PbiWorkspaceCollections = Get-AzureRmPowerBIWorkspaceCollection -ResourceGroupName $ResourceGroupName `
                                                                 -WorkspaceCollectionName $WorkspaceCollectionName

作成したワークスペースコレクションの アクセスキーを取得する

#Get API keys for a Workspace Collection
$ApiKeys = Get-AzureRmPowerBIWorkspaceCollectionAccessKeys -ResourceGroupName $ResourceGroupName `
                                                           -WorkspaceCollectionName $WorkspaceCollectionName
$AppKey = $ApiKeys.GetValue(0).Value

ワークスペースを作成する

この手順から REST経由で Power BI Embedded を操作します。
AzureRM.PowerBIEmbedded

#New PowerBI Workspace
#https://msdn.microsoft.com/en-us/library/azure/mt711503.aspx
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces"
$Method = "POST"
$Header = @{
    Authorization  = "AppKey $AppKey"
    'Content-Type' = 'application/json'
}
Invoke-RestMethod -Uri $Uri `
                  -Method $Method `
                  -Headers $Header `
                  -Body $Body

#Get PowerBI Workspaces
$PbiWorkspaces = Get-AzureRmPowerBIWorkspace -ResourceGroupName $ResourceGroupName `
                                             -WorkspaceCollectionName $WorkspaceCollectionName
#WorkspaceId
$WorkspaceId = $PbiWorkspaces.Name

Power BI Desktop を使用して .pbix ファイルを作成する

この手順はPowerShellでは実施できませんので割愛します。

.pbix ファイルをワークスペースにインポートする

上の手順で作成した .pbix ファイルをインポートします。
Powershell ではHTTP の マルチパートPOSTリクエストができなかったので Powershell から curl を呼び出しました。

#Import PBIX file into a workspace
#https://msdn.microsoft.com/en-us/library/azure/mt711504.aspx
$PbixFilePath          = "C:\temp\sample.pbix"
$datasetDisplayName    = "mydataset01"
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces/$WorkspaceId/imports?datasetDisplayName=$datasetDisplayName"
$Header = "Authorization:AppKey $AppKey"

curl.exe -H "$Header" -F "file=@$PbixFilePath" "$Uri"

データソース接続

接続文字列を設定する際に必要なデータセットIDを取得します。

#Get Datasets
#https://msdn.microsoft.com/ja-jp/library/mt203567.aspx
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces/$WorkspaceId/datasets"
$Method = "GET"
$Header = @{
    Authorization  = "AppKey $AppKey"
    'Content-Type' = 'application/json'
}
$Response = Invoke-RestMethod -Uri $Uri `
                              -Method $Method `
                              -Headers $Header

#DatasetId
$DatasetId = $Response.value.id

接続文字列を設定します。

#Set Connectionstring
#https://msdn.microsoft.com/en-us/library/azure/mt711505.aspx
$ConnectionString = "data source=pbe-test1-dbserver1.database.windows.net,1433;initial catalog=testdb;persist security info=False;"
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces/$WorkspaceId/datasets/$DatasetId/Default.SetAllConnections"
$Method = "POST"
$Header = @{
    Authorization  = "AppKey $AppKey"
    'Content-Type' = 'application/json'
}
$Body = @{
    connectionString = $ConnectionString
} | ConvertTo-Json
Invoke-RestMethod -Uri $Uri `
                  -Method $Method `
                  -Headers $Header `
                  -Body $Body

レポートをWebページに埋め込むために必要なレポートIDを取得します。

#Get Reports
#https://msdn.microsoft.com/en-us/library/azure/mt711510.aspx
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces/$WorkspaceId/reports"
$Method = "GET"
$Header = @{
    Authorization  = "AppKey $AppKey"
    'Content-Type' = 'application/json'
}
$Response = Invoke-RestMethod -uri $Uri `
                              -Method $Method `
                              -Headers $Header

#ReportId
$Response.value.id

データソースの資格情報を変更する

データソースの資格情報を変更するために必要なゲートウェイIDを取得します。

#Get gatewayId
#https://msdn.microsoft.com/en-us/library/azure/mt711500.aspx
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces/$WorkspaceId/datasets/$DatasetId/Default.GetBoundGatewayDatasources"
$Method = "GET"
$Header = @{
    Authorization  = "AppKey $AppKey"
    'Content-Type' = 'application/json'
}
$Response = Invoke-RestMethod -Uri $Uri `
                              -Method $Method `
                              -Headers $Header
$GatewayId    = $Response.value.gatewayId
$DatasourceId = $Response.value.Id

データソースの資格情報を変更します。

#Set Credential
#https://msdn.microsoft.com/en-us/library/azure/mt711498.aspx
$Uri    = "https://api.powerbi.com/v1.0/collections/$WorkspaceCollectionName/workspaces/$WorkspaceId/gateways/$GatewayId/datasources/$DatasourceId"
$Method = "PATCH"
$Header = @{
    Authorization  = "AppKey $AppKey"
    'Content-Type' = 'application/json'
}
$Body = @{
    credentialType   = "Basic"
    basicCredentials = @{
        username = "dummyuser"
        password = "dummypass"
    }
} | ConvertTo-Json
Invoke-RestMethod -Uri $Uri `
                  -Method $Method `
                  -Headers $Header `
                  -Body $Body

完成

ここまでで、必要な設定と情報の取得ができましたので、最後にレポートをWebページに埋め込んで完成です。
赤枠で囲った部分が iframeで読み込んだ部分です。
f:id:su-zuki:20170623184032p:plain

いいですね!画像なので伝わらないですが、クリックしてフィルターや期間の条件を指定すると、グラフがヌルヌル動きます。

ありがとうございました。