0

下面的 powershell 脚本通过调用 Power BI rest api 获取 authtoken 并刷新所有 Power BI 数据集。该脚本在 powershell ISE 中运行良好,但无法在 vscode 中运行。我已尝试安装多个 Azure 扩展,但均无济于事。我对 vscode 和 powershell 都比较陌生,可以就如何从这里继续前进提出一些建议。

# https://technovert.com/refreshing-all-datasets-in-power-bi-using-rest-api/
# https://github.com/Azure-Samples/powerbi-powershell/blob/master/manageRefresh.ps1

$clientId = “78xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxa4” #Client Id of the registered app.

function GetAuthToken {
       
    if(-not (Get-Module AzureRm.Profile)) {

        Import-Module AzureRm.Profile
    }
 
    $redirectUri = "urn:ietf:wg:oauth:2.0:oob"
 
    $resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
 
    $authority = "https://login.microsoftonline.com/common/oauth2/authorize";
 
    $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
 
    $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
 
    return $authResult
}

$token = GetAuthToken

$authHeader = @{

    'Content-Type'='application/json'

    'Authorization'= $token.CreateAuthorizationHeader()
}

$groupsPath = ""

if ($groupID -eq "me") {

    $groupsPath = "myorg"

} else {

    $groupsPath = "myorg/groups/"
} 

$uril = "https://api.powerbi.com/v1.0/$groupsPath"

$restResponse1 = Invoke-RestMethod -Uri $uril -Headers $authHeader -Method GET 

$x=$restResponse1.value

Write-Host "`n"

foreach($i in $x) {

    $groupID=$i.Id #workspace Id

    $groupName = $i.Name #Workspace name

    #$groupName + "-" + $groupID
    Write-Host "Refreshing Workspace: $groupName, Id: $groupID `n" -ForegroundColor Yellow

    $uri = "https://api.powerbi.com/v1.0/$groupsPath/$groupID/datasets" 

    $restResponse = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET 

    $d=$restResponse.value

    foreach($j in $d) {

        $datasetID=$j.Id #dataset Id

        $datasetName=$j.Name #dataset Name

        #$datasetName + "-" + $datasetID
        Write-Host "    Refreshing dataset: $datasetName, Id: $datasetID `n"

        # Refresh the dataset
        $uri = "https://api.powerbi.com/v1.0/$groupsPath/$groupID/datasets/$datasetID/refreshes" 

        $postResponse = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST

        Start-Sleep -s 10

        # Check the refresh history
        $uri = "https://api.powerbi.com/v1.0/$groupsPath/$groupID/datasets/$datasetID/refreshes"

        $getResponse = Invoke-RestMethod -Uri $uri –Headers $authHeader –Method GET

        Start-Sleep -s 30
    }
}
4

1 回答 1

1

根据您的错误消息,您似乎正面临模块程序集依赖冲突。

请在 VS Code 中检查您的 .NET Core版本。

这是您可以参考的模块程序集依赖冲突的类似问题。

在 NETCoreApp 1.0 和 1.1 中,Microsoft.NETCore.Portable.Compatibility 包增加了对基于 mscorlib 的可移植类库的支持。这些类库与某些桌面程序集具有相同的参考程序集名称:mscorlib、system、system.core 等,但它们的版本与可移植配置文件的所有受支持框架的最低版本相匹配:silverlight。

于 2020-10-19T08:50:39.943 回答