【问题标题】:PowerShell Code for Power BI APIs in Azure Automation RunbookAzure 自动化 Runbook 中 Power BI API 的 PowerShell 代码
【发布时间】:2020-11-01 13:48:15
【问题描述】:

我们使用常规 PowerShell 在域服务器上成功执行了以下代码。但是,当我们在 Azure 自动化运行手册中执行此 PowerShell 代码时,我们会收到一条消息“Suspended Runbook 作业尝试了 3 次,但每次都失败。"

我们无法使用服务主体,因为您无法从服务主体执行任何管理 API。因此,我们需要使用普通用户帐户来登录 Power BI API。

有什么想法可以让这段代码在 Runbook 中运行吗?

$UserName = 'user01@sample.com'
$User_Password = 'xx@#xsrasda!@W12'
$User_Password_Secure = ConvertTo-SecureString -AsPlainText $User_Password -Force

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, 

Connect-PowerBIServiceAccount  -Credential $Credential
    if($SuppressErrors -eq $false) 
    {
        Resolve-PowerBIError -Last
    }

Invoke-PowerBIRestMethod -Method Get -Url 'admin/reports'

Disconnect-PowerBIServiceAccount

【问题讨论】:

    标签: api automation powerbi runbook


    【解决方案1】:

    我推荐this article by Gerhard Brueckl,他展示了如何使用 Invoke-RestMethod 绕过 Power BI cmdlet。这是我发现让 Azure Runbook 工作的唯一方法。他也提供了working sample

    您的代码将与此类似。但请注意,您将需要创建一个 Azure 应用注册并使用该客户端 ID。 https://dev.powerbi.com/apps 有一个向导可以创建它。

           $body = @{    
                "resource" = "https://analysis.windows.net/powerbi/api";
                "client_id" = $clientId;
                "grant_type" = "password";
                "username" = $UserName ;
                "password" = $User_Password;
                "scope" = "openid"
            }
            $resp = Invoke-RestMethod -Uri $authUrl -Method POST -Body $body
            
             $authHeaders =   @{
                "Content-Type" = "application/json";
                "Authorization" = $authResponse.token_type + " " + $authResponse.access_token
            } 
    
                Invoke-RestMethod -Method Get -Headers $authHeaders "https://api.powerbi.com/v1.0/myorg/admin/reports"
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2019-05-17
      • 2020-07-24
      • 2021-12-24
      • 2020-12-09
      • 1970-01-01
      • 2021-06-23
      相关资源
      最近更新 更多