Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Trying to use Rest API to Refresh power BI data source via PowerShell and Gateway

Hi Everyone,

I'm trying to use the example code posted here to refresh my data sources in power BI through an established gateway via rest API. I am able to copy and paste the code into the PowerShell prompt window and it works but when I try to run it as a script it doesn't work. Even when I know the proper libraries are installed I continue to get the error: 

 

Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'â€Headers
System.Collections.Hashtable â€Method'.
At \\BN-NDIBISRVR01\Shared\PowerBI_Powershell_Script\Updated_Powershell_Script.ps1:119 char:1
+ Invoke-RestMethod -Uri $uri –Headers $authHeader –Method POST – ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

I appreciate any help you can provide! I have included my code below but removed the dataset ID and client ID.

Set-ExecutionPolicy RemoteSigned

Import-Module AzureRM


$groupID = "me" # the ID of the group that hosts the dataset. Use "me" if this is your My Workspace

$datasetID = "730"
$clientId = "10" 

function GetAuthToken
{
       $adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
 
       $adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
 
       [System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
 
       [System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
 
       $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
}

# Get the auth token from AAD
$token = GetAuthToken

# Building Rest API header with authorization token
$authHeader = @{
   'Content-Type'='application/json'
   'Authorization'=$token.CreateAuthorizationHeader()
}

# properly format groups path
$groupsPath = ""
if ($groupID -eq "me") {
    $groupsPath = "myorg"
} else {
    $groupsPath = "myorg/groups/$groupID"
}

# refresh Field Booking Trends
# Refresh the dataset
$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method POST –Verbose


  • Anonymous's avatar
    Anonymous
    8 years ago

    Hi, 

    I agree that it's a powershell issue but I thought I would post here since it was in relation to Power BI. I figured out the issue on my own. These are the two pieces of code I had to update to make the sample work (in case anyone is interested in doing this too):

     

    $authHeader = @{
    Authorization=$token.CreateAuthorizationHeader()
    Content='application/json'
    }

     

    long dashes to short dashes:

    Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST -Verbose

2 Replies

  • v-ljerr-msft's avatar
    v-ljerr-msft
    Microsoft Employee

    Hi Anonymous,

     

    It seems to be that the issue is more related PowerShell, So I would suggest post it on PowerShell forum for better assistance. :smileyhappy:

     

    In addition, here is a similar thread for your reference.

     

    Regards

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, 

      I agree that it's a powershell issue but I thought I would post here since it was in relation to Power BI. I figured out the issue on my own. These are the two pieces of code I had to update to make the sample work (in case anyone is interested in doing this too):

       

      $authHeader = @{
      Authorization=$token.CreateAuthorizationHeader()
      Content='application/json'
      }

       

      long dashes to short dashes:

      Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST -Verbose