Azure-Samples / powerbi-powershell

Samples for calling the Power BI REST API via PowerShell
MIT License
256 stars 189 forks source link

MS PowerBI Rest error: Invoke-RestMethod : The 'Content-Type' header must be modified #5

Open NancyAb opened 6 years ago

NancyAb commented 6 years ago

Hi,

I have PowerBI setup with a gateway and have been able to refresh the datasource using the MS tools.

I have customized the ps1 program with my company's settings and have installed the PowerShell tools.

When I run the manageRefresh.ps1 command, I get the errors:

Invoke-RestMethod : The 'Content-Type' header must be modified using the appropriate property or method.
Parameter name: name
At C:\temp\manageRefresh.ps1:78 char:1
+ Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The 'Content-Type' header must be modified using the appropriate property or method.
Parameter name: name
At C:\temp\manageRefresh.ps1:82 char:1
+ Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Any ideas? Thanks in advance for looking. Nancy.

BrightKnight6 commented 6 years ago

I am getting a similar error:

Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'â€Headers
System.Collections.Hashtable â€Method'.
At \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
BrightKnight6 commented 6 years ago

@NancyAb

I was getting the same error and these updates made my code work: $authHeader = @{ Authorization=$token.CreateAuthorizationHeader() Content='application/json' }

and I believe the sample code mixes long and short dashes which was giving me an error as well so here is the modified code for that: Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST -Verbose

NancyAb commented 6 years ago

Thanks so much. I will have a look. Nancy.

sjt003001 commented 6 years ago

Hi,

I had the same issue and got an error with BrightKnight6 code too, but adding single quotes around Authorization and Content seemed to do the trick. So the key difference between the original and this is Content instead of Content-Type. I'm a newbie to all the concepts here so can't really explain why.

This is what I ended up with:

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

Scott

CorinnaAdkins commented 6 years ago

I consistently receive the error below and my dataset does not refresh, any help would be appreciated. " Invoke-RestMethod : The remote server returned an error: (400) Bad Request. At line:85 char:1

VERBOSE: GET https://api.powerbi.com/v1.0/myorg/datasets/$datasetId/refreshes with 0-byte payload

ashu16815 commented 6 years ago

HI All,

I am also getting : PS C:\temp> .\sample.ps1 [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult System.Collections.Hashtable Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'â?Headers System.Collections.Hashtable â?Method'. At C:\temp\sample.ps1:78 char:1

I tried all changes mentioned above but nothing worked. Please help

chadtoney commented 5 years ago

I'm also getting similar error:

Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'â€Headers System.Collections.Hashtable â€Method'. At C:\Users\toneyc\Downloads\copyWorkspace.ps1:76 char:16

nickodemis99 commented 4 years ago

HI All,

I am also getting : PS C:\temp> .\sample.ps1 [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult System.Collections.Hashtable Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'â?Headers System.Collections.Hashtable â?Method'. At C:\temp\sample.ps1:78 char:1

  • Invoke-RestMethod -Uri $uri â?"Headers $authHeader â?"Method POST â?"Verbose
  • + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand](url)

I tried all changes mentioned above but nothing worked. Please help

You need to save you code to file that is not UTC. More than likely you copied code from a webpage and it include special characters that need to be removed.

You can always delete the problem line and retype it manually.

Shailevy commented 4 years ago

+1

Tried all suggestions and nothing worked.

@nickodemis99 The file was download via git, not a web browser