Forum Discussion
update-refresh-schedule-in-group error 415 Unsupported media type using powershell
Hello everyone,
Using the script below I try to update a dataset refresh schedule I Think this thing is OK I tried all kind of variations on it. but it keeps responding Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type. at Documents\Powershell\UpdateRefreshSchedule.ps1:33 char:13
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I did other scripts with POST that also included a body and the same header and those worked.
As well as I am the owner of the schedule
any ideas how to fix this will be apreciated.
# https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/update-refresh-schedule-in-group
## once in a lifetime you got to do this:
## Install-Module -Name MicrosoftPowerBIMgmt
## somtimes in a lifetime you got to do this:
## Update-Module -Name MicrosoftPowerBIMgmt
## Depending on your execution policy do not forget to trust the session
## Set-ExecutionPolicy -ExecutionPolicy remotesigned -Scope Process
## login to the platform
Login-PowerBI
#Get a token
$headers = Get-PowerBIAccessToken
#Fill in your workspace and dataset id below
$workspaceid = '74601087-ad5e-481c-a20c-1965baa8607b'
$datasetid = 'd469f67e-4568-49eb-b6bc-e42f2c5d37d5'
#Create a json body
$Body = @{
"value"= [ordered]@{
"days"= @("Sunday")
"times"=@("08:00","08:30")
"enabled"=$false
"localTimeZoneId"=@("Central Standard Time")
"notifyOption"=@("MailOnFailure")
}} | ConvertTo-Json
#change the schedule dataset
$uri = (ForEach-Object {'https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/refreshSchedule' -F $workspaceid,$datasetid })
$response = Invoke-RestMethod -Uri $uri -Method "PATCH" -Headers $headers -Body $Body
https://github.com/mschoombee/Blog/blob/main/2020/automating-power-bi-deployments/Reports%20-%20Update%20Refresh%20Schedule.ps1
on this github site I found the solution.# https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/update-refresh-schedule-in-group ## once in a lifetime you got to do this: ## Install-Module -Name MicrosoftPowerBIMgmt ## somtimes in a lifetime you got to do this: ## Update-Module -Name MicrosoftPowerBIMgmt ## Depending on your execution policy do not forget to trust the session ## Set-ExecutionPolicy -ExecutionPolicy remotesigned -Scope Process ## login to the platform Login-PowerBI ##Build the body $ApiRequestBody = @" { "value": { "days": [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], "times": [ "08:00", "08:30" ], "notifyOption": "MailOnFailure", "localTimeZoneId": "UTC", "enabled": true } } "@ ## Invoke the API but do not include the header Invoke-PowerBIRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/groups/74601087-ad5e-481c-a20c-1965baa8607b/datasets/d469f67e-4568-49eb-b6bc-e42f2c5d37d5/refreshSchedule' -Method Patch -Body ("$ApiRequestBody")I tried to schedule between full and half hours but then the response is
Invoke-PowerBIRestMethod : Encountered errors when invoking the command: {
"code": "InvalidRequest",
"message": "Refresh schedule time must be full or half hour (HH:00 or HH:30)" so if you can live with that this is the solution
3 Replies
- AnonymousNot applicable
Hi SofBL ,
I suggest you to try code as below.
Invoke-PowerBIRestMethod -Url 'groups/groupid.../datasets/datasetid.../refreshSchedule' -Method PATCH -Body ([pscustomobject]@{ "value"= [ordered]@{ "days"= @("Sunday") "times"= @("08:00","16:00") } } | ConvertTo-Json -Depth 2 -Compress)For reference: Power BI Cmdlets reference | Microsoft Learn
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- SofBLAdvocate II
Thanks for helping! The biggest error I made is that I included the -headers and that is not necessary Secondly I used the Invoke-RestMethod, you suggest using Invoke-PowerBIRestMethod
according to the docsInvoke-RestMethod likes to have the -body formatted as an <object> and does not need the -headers
Invoke-PowerBIRestMethod wants the -body as an <string> and needs the header
- SofBLAdvocate II
https://github.com/mschoombee/Blog/blob/main/2020/automating-power-bi-deployments/Reports%20-%20Update%20Refresh%20Schedule.ps1
on this github site I found the solution.# https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/update-refresh-schedule-in-group ## once in a lifetime you got to do this: ## Install-Module -Name MicrosoftPowerBIMgmt ## somtimes in a lifetime you got to do this: ## Update-Module -Name MicrosoftPowerBIMgmt ## Depending on your execution policy do not forget to trust the session ## Set-ExecutionPolicy -ExecutionPolicy remotesigned -Scope Process ## login to the platform Login-PowerBI ##Build the body $ApiRequestBody = @" { "value": { "days": [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], "times": [ "08:00", "08:30" ], "notifyOption": "MailOnFailure", "localTimeZoneId": "UTC", "enabled": true } } "@ ## Invoke the API but do not include the header Invoke-PowerBIRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/groups/74601087-ad5e-481c-a20c-1965baa8607b/datasets/d469f67e-4568-49eb-b6bc-e42f2c5d37d5/refreshSchedule' -Method Patch -Body ("$ApiRequestBody")I tried to schedule between full and half hours but then the response is
Invoke-PowerBIRestMethod : Encountered errors when invoking the command: {
"code": "InvalidRequest",
"message": "Refresh schedule time must be full or half hour (HH:00 or HH:30)" so if you can live with that this is the solution