What vscode extensions do I need to run this powershell code?

388 views Asked by At

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
    }
}
1

There are 1 answers

0
Doris Lv On

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.