简体   繁体   English

Power Bi 订阅

[英]Power Bi subscriptions

I want to run power bi subscription in power shell command.我想在 power shell 命令中运行 power bi 订阅。 I am new to that subscription concept.我对那个订阅概念很陌生。 Can anyone give the clear explanation about this.任何人都可以对此给出明确的解释。 thanks in advance.提前致谢。

You cannot programmatically trigger e-mail subscriptions .您不能以编程方式触发电子邮件订阅 They are running according their schedule.他们按照自己的时间表运行。 But you can use Export to File API to export the report to a PDF file (or some other supported file type ) and send it to someone.但是您可以使用导出到文件 API将报告导出到 PDF 文件(或其他一些支持的文件类型)并将其发送给某人。 This means that the workspace must be attached to a dedicated capacity ( Power BI Premium P SKU, Power BI Premium EM SKU or Power BI Embedded A SKU , and probably Premium per user ) and you should maintain the list of "subscribers" on your own.这意味着工作区必须附加到专用容量( Power BI Premium P SKU、Power BI Premium EM SKUPower BI Embedded A SKU ,可能还有Premium per user ),并且您应该自己维护“订阅者”列表.

Here is some PowerShell code, which uses Power BI Management CmdLets , which will export a report to PDF and send it by e-mail.这是一些 PowerShell 代码,它使用Power BI 管理 CmdLets ,它将报告导出到 PDF 并通过电子邮件发送。 You need to edit the parameters in the beginning of the code, like report ID, workspace ID, credentials, recipients, etc.您需要编辑代码开头的参数,例如报告 ID、工作区 ID、凭据、收件人等。

#############################################################
# Install-Module -Name MicrosoftPowerBIMgmt
# Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser
#############################################################
Import-Module MicrosoftPowerBIMgmt

#############################################################
# Power BI settings
#############################################################
$password = "SuperStrongPassword" | ConvertTo-SecureString -asPlainText -Force
$username = "account@example.com" 

#############################################################
# Report settings
#############################################################
$groupId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$reportId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$format = "PDF" # or PNG, or PPTX. For paginated reports can be CSV, DOCX, IMAGE (i.e. page definition plus file format - BMP, EMF, etc.), MHTML, XLSX or XML
$saveToFolder = "D:\"
$fileSuffix = " (" + (Get-Date).ToString("yyyy-MM-dd HH-mm-ss") + ")"


#############################################################
# E-mail settings
#############################################################
$emailSmtpServer = "smtp.office365.com"
$emailSmtpServerPort = 587
$emailTo = $username
$emailSubject = "Here is your report"
$emailBodyHtml = "Please find <b>your report</b> attached here."
$emailUser = "account@example.com"
$emailPassword = "SuperStrongPassword"
$emailFrom = $emailUser


$credential = New-Object System.Management.Automation.PSCredential($username, $password)

Connect-PowerBIServiceAccount -Credential $credential



$settings = @{ includeHiddenPages = $false; locale = "en-us" }
$powerBIReportConfiguration = @{ settings = $settings }
$export_body = @{ format = $format; powerBIReportConfiguration = $powerBIReportConfiguration }
#'{
#  format: "$format",
#  powerBIReportConfiguration: { settings: { includeHiddenPages: false, locale: "en-us" } }
#}'

$export_response = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/groups/$groupId/reports/$reportId/ExportTo" -Method Post -Body ($export_body | ConvertTo-Json)

$export_response_json = ConvertFrom-Json $export_response

$exportId = $export_response_json.id

Write-Output "Polling export status..."
$maxPollCount = 500
$exportSucceeded = $false
do
{
    $maxPollCount = $maxPollCount - 1

    Start-Sleep -Seconds 5

    $status_response = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/groups/$groupId/reports/$reportId/exports/$exportId" -Method Get

    $status_response_json = ConvertFrom-Json $status_response
    $status = $status_response_json.status
    $percentComplete = $status_response_json.percentComplete

    Write-Output "Status: $status, percent complete: $percentComplete (retries left: $maxPollCount)"

    if ($status -eq "Succeeded")
    {
        $exportSucceeded = $true
        $resourceLocation = $status_response_json.resourceLocation
        $reportName = $status_response_json.reportName
        $resourceFileExtension = $status_response_json.resourceFileExtension
        $outFile = [IO.Path]::Combine($saveToFolder, $reportName + $fileSuffix + $resourceFileExtension)
    }
}
until($exportSucceeded -or $maxPollCount -le 0)


Write-Output "Downloading export..."
$download_response = Invoke-PowerBIRestMethod -Url $resourceLocation -Method Get -OutFile $outFile
Write-Output "Download completed."


Disconnect-PowerBIServiceAccount


$emailCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $emailUser, $($emailPassword | ConvertTo-SecureString -AsPlainText -Force)

Send-MailMessage -From $emailFrom `
    -To $emailTo `
    -Subject $emailSubject `
    -Body $emailBodyHtml `
    -BodyAsHtml `
    -SmtpServer $emailSmtpServer `
    -Credential $emailCredentials `
    -UseSsl `
    -Attachments $outFile 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM