简体   繁体   中英

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.

The site script runs without errors but does not return any results in CSV, is it because the URL is wrong?

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.

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.

Powershell script:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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