【问题标题】:What vscode extensions do I need to run this powershell code?我需要哪些 vscode 扩展来运行这个 powershell 代码?
【发布时间】:2020-10-19 06:27:50
【问题描述】:

下面的 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
    }
}

【问题讨论】:

  • “不会运行”究竟是什么意思?什么都没发生?错误?弹出窗口说“我不会在 vs 代码中运行”?
  • 我收到这样的错误:行 | 20 | $authResult = $authContext.AcquireToken($resourceAppIdURI, $clien ... | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 使用“4”参数调用“AcquireToken”的异常:“无法加载类型'System.Security.Cryptography.SHA256Cng' 来自程序集 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'。”
  • 脚本的工作不依赖于 VS Code 扩展(这些与此无关)。您可能缺少一些参考程序集。 PS ISE 默认会加载很多程序集(Winforms、WPF 等),而 VS Code 不会。您所要做的就是使用 Add-Type -AssemblyName 包含程序集

标签: azure powershell visual-studio-code powerbi-api


【解决方案1】:

根据您的错误消息,您似乎面对的是module assembly dependency conflicts.

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

这里有一个similar issue,有模块组装依赖冲突,你可以参考。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多