简体   繁体   English

从 PowerShell 从 Power BI REST API 获取组内刷新历史记录并导出 CSV

[英]From PowerShell Get Refresh History In Group from Power BI REST APIs and export do CSV

I'm trying to get the PowerBI update history through the API URL through Powershell and export do CSV, I don't know much about Powershell but I'm trying to learn, since it's being very useful in the BI area where I work, I've tried to run the code through this site POWERBI SCHEDULED REFRESHES IN YOUR ORGANIZATION and also tried to edit to extract but with no success. I'm trying to get the PowerBI update history through the API URL through Powershell and export do CSV, I don't know much about Powershell but I'm trying to learn, since it's being very useful in the BI area where I work,我已尝试通过此站点POWERBI 计划刷新您的组织运行代码,并尝试编辑以提取但没有成功。

The site script runs without errors but does not return any results in CSV, is it because the URL is wrong?站点脚本运行没有错误,但在 CSV 中没有返回任何结果,是不是因为 URL 错误?

Website script:网站脚本:

$myCred = Get-Credential
Connect-PowerBIServiceAccount -Credential $myCred

$Workspaces = Get-PowerBIWorkspace
$ExportFile =  "D:\Users\F02579\Documents\PowerBIRefreshHistory.csv"
Remove-Item $ExportFile -Force -ErrorAction SilentlyContinue

foreach($workspace in $Workspaces)
{

    $DataSets = Get-PowerBIDataset -WorkspaceId $workspace.Id | where {$_.isRefreshable -eq $true}
    foreach($dataset in $DataSets)
    {

        $URI = "groups/" + $workspace.id + "/datasets/" + $dataset.id + "/refreshes"
        #$OutFile = $ExportFolder + '\' + $workspace.Name + '-' + $dataset.Name + '.json'
        $Results = Invoke-PowerBIRestMethod -Url $URI -Method Get | ConvertFrom-Json

        foreach($result in $Results.value)
        {
            $errorDetails = $result.serviceExceptionJson | ConvertFrom-Json -ErrorAction SilentlyContinue

            $row = New-Object psobject
            $row | Add-Member -Name "Workspace" -Value $workspace.Name -MemberType NoteProperty
            $row | Add-Member -Name "Dataset" -Value $dataset.Name -MemberType NoteProperty
            $row | Add-Member -Name "refreshType" -Value $result.refreshType -MemberType NoteProperty
            $row | Add-Member -Name "startTime" -Value $result.startTime -MemberType NoteProperty
            $row | Add-Member -Name "endTime" -Value $result.endTime -MemberType NoteProperty
            $row | Add-Member -Name "status" -Value $result.status -MemberType NoteProperty
            $row | Add-Member -Name "errorCode" -Value $errorDetails.errorCode -MemberType NoteProperty
            $row | Add-Member -Name "errorDescription" -Value $errorDetails.errorDescription -MemberType NoteProperty
            $row | Export-Csv -Path $ExportFile -Append -Delimiter ';' -NoTypeInformation
        }

    }

}

I tried to put the URL that Microsoft provides in this link, but also without success.我试图把微软提供的URL放在这个链接里,但也没有成功。

My script:我的脚本:

Import-Module MicrosoftPowerBIMgmt.Admin

$myCred = Get-Credential
Connect-PowerBIServiceAccount -Credential $myCred

$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
    -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
    -Credential $credential `
    -Authentication Basic `
    -AllowRedirection

Import-PSSession $Session

$Workspaces = Get-PowerBIWorkspace -Scope Organization -All
#Remove-Item $ExportFile -Force -ErrorAction SilentlyContinue

$DataSets =
ForEach ($workspace in $Workspaces)
    {
    Write-Host $workspace.Name
    ForEach ($dataset in (Get-PowerBIDataset -Scope Organization -WorkspaceId $workspace.Id))
        {
            #$URI = "groups/" + $workspace.id + "/datasets/" + $dataset.id + "/refreshes"
            $URI = "https://api.powerbi.com/v1.0/myorg/groups/" + $workspace.id + "/datasets/" + $dataset.id + "/refreshes"
            #$OutFile = $ExportFolder + '\' + $workspace.Name + '-' + $dataset.Name + '.json'
            $Results = Invoke-PowerBIRestMethod -Url $URI -Method Get | ConvertFrom-Json

            foreach($result in $Results.value)
            {
                $errorDetails = $result.serviceExceptionJson | ConvertFrom-Json -ErrorAction SilentlyContinue

                $ItemResult = New-Object System.Object
                $ItemResult | Add-Member -type NoteProperty -name WorkspaceID           -value $workspace.Id
                $ItemResult | Add-Member -type NoteProperty -name DatasetID             -value $dataset.Id
                $ItemResult | Add-Member -type NoteProperty -name RefreshID             -value $result.Id
                $ItemResult | Add-Member -type NoteProperty -name RequestId             -value $result.requestId
                $ItemResult | Add-Member -type NoteProperty -name RefresheType          -Value $result.refreshType              
                $ItemResult | Add-Member -type NoteProperty -name StartTime             -Value $result.startTime                
                $ItemResult | Add-Member -type NoteProperty -name EndTime               -Value $result.endTime                  
                $ItemResult | Add-Member -type NoteProperty -name Status                -Value $result.status                   
                $ItemResult | Add-Member -type NoteProperty -name ErrorCode             -Value $errorDetails.errorCode          
                $ItemResult | Add-Member -type NoteProperty -name ErrorDescription      -Value $errorDetails.errorDescription       

                # Put the item result and append it to the result object
                $Result +=$ItemResult
            }
        }
    }
$DataSets | Export-Csv "D:\Users\F02579\Documents\PowerBIRefreshHistory.csv" -NoTypeInformation -Encoding UTF8

Remove-PSSession $Session

Disconnect-PowerBIServiceAccount

Error on my scripp:我的脚本错误:

PowerShell错误

After making some calls with Microsoft, with some engineers that works with PowerBI and Powershell conector, I'm now able to get those information.在与 Microsoft 打了一些电话之后,与一些使用 PowerBI 和 Powershell 连接器的工程师一起,我现在能够获得这些信息。

Powershell script: Powershell 脚本:

$authUrl = "https://login.windows.net/common/oauth2/token/"
$clientId = "Your clientID"
$pbiUsername = "Your user email"
$pbiPassword = "Your password"

$body = @{
    "resource" = “https://analysis.windows.net/powerbi/api";
    "client_id" = $clientId;
    "grant_type" = "password";
    "username" = $pbiUsername;
    "password" = $pbiPassword;
    "scope" = "openid"
}
$authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body

$DatasetID = "Your datasetID"
$restURL = "https://api.powerbi.com/v1.0/myorg/datasets/$DatasetID/refreshes"

$headers = @{
    "Content-Type" = "application/json";
    "Authorization" = $authResponse.token_type + " " + $authResponse.access_token
}

$restResponse = Invoke-RestMethod -Uri $restURL –Method GET -Headers $headers | Select-Object -ExpandProperty value
$restResponse

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

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