0

The below powershell script acquires an authtoken and refreshes all Power BI datasets with a call to the Power BI rest api. The script works fine in powershell ISE but won't run in vscode. I have tried installing several Azure extensions to no avail. I am relatively new to both vscode and powershell and could use some advice on how to move on from here.

# 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
    }
}
Mike
  • 99
  • 1
  • 10
  • What does “won’t run” mean precisely? Nothing happens? Error? Pop up says “I won’t run in vs code”? – Doug Maurer Oct 19 '20 at 06:38
  • I get errors like this: Line | 20 | $authResult = $authContext.AcquireToken($resourceAppIdURI, $clien … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Exception calling "AcquireToken" with "4" argument(s): "Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'." – Mike Oct 19 '20 at 06:39
  • 3
    The working of scripts does not depend on VS Code extensions (these have nothing to do with this). You are propably missing some reference assemblies. PS ISE loads a lot of assemblies by default (Winforms, WPF etc) which VS Code does not. All you have to do is include the assemblies by using Add-Type -AssemblyName – bluuf Oct 19 '20 at 07:05

1 Answers1

1

According to your error message, it seems you are facing with module assembly dependency conflicts.

Please check your .NET Core version in VS Code.

Here is a similar issue with module assembly dependency conflicts you can refer to.

In NETCoreApp 1.0 and 1.1 the Microsoft.NETCore.Portable.Compatibility package added support for mscorlib-based portable class libraries. These class libraries have the same reference assembly names as some desktop assemblies: mscorlib, system, system.core, etc, but their versions match the lowest version of all supported frameworks for the portable profiles: silverlight.

Doris Lv
  • 3,083
  • 1
  • 5
  • 14