Forum Discussion

HassanRizvi's avatar
HassanRizvi
New Member
1 year ago
Solved

Forbidden (403) error when using While statement in PowerShell URL

Hi,

I am kind of new to the realm of PowerBI, and I am trying to make a real-time dashboard in Power BI service with a dummy dataset.

I am using the streaming dataset module in PowerBi Service and Using the API tab to create a dataset. I defined three variables Date(DateTime), Name(Text) and Marks(Number). And received the following PowerShell API code

 

 

 

 

$endpoint = "https://api.powerbi.com/beta/f39f15ca-ca8b-4f09-af08-8e6117450991/datasets/c6da6023-0fd5-41fa-ad07-64e8f3505d68/rows?experience=power-bi&clientSideAuth=0&key=EOA9pzRzr%2BtpKXqjI6ludXliGF%2FI8xiiNx6d3Z7yy02STWFAevQWqMXxIySExzMDD70YkVWDmC4oSRIWvj%2Bulg%3D%3D"
$payload = @{
"Date" ="2025-03-04T04:54:52.050Z"
"Name" ="AAAAA555565"
"Marks" =98.6
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))

 

 

 

 

Now I want to create a while loop in this code to auto-update my report and create a real-time dashboard; for this I took the help of ChatGpt to devise a code that can help. I received the following code for this reason.

 

 

 

 

$endpoint = "https://api.powerbi.com/beta/f39f15ca-ca8b-4f09-af08-8e6117450991/datasets/c6da6023-0fd5-41fa-ad07-64e8f3505d68/rows?experience=power-bi&clientSideAuth=0&key=EOA9pzRzr%2BtpKXqjI6ludXliGF%2FI8xiiNx6d3Z7yy02STWFAevQWqMXxIySExzMDD70YkVWDmC4oSRIWvj%2Bulg%3D%3D"
$refreshEndpoint = "https://api.powerbi.com/v1.0/myorg/datasets/c6da6023-0fd5-41fa-ad07-64e8f3505d68/refreshes"

while ($true) {
    # Update data
    $payload = @{
        "Date" = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")  # Current date and time
        "Name" = "AAAAA555555"
        "Marks" = Get-Random -Minimum 0 -Maximum 100  # Random marks between 0 and 100
    }

    Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))

    # Trigger dataset refresh
    Invoke-RestMethod -Method Post -Uri "$refreshEndpoint" -Body (ConvertTo-Json @{}) -Headers @{
        "Content-Type" = "application/json"
        # Include your authorization token here if required
        "Authorization" = "Bearer YOUR_ACCESS_TOKEN"
    }

    Start-Sleep -Seconds 10  # Wait for 10 seconds before the next iteration
}

 

 

 

 So, when I punched the code, the following error emerged,

 

 

 

Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
At line:15 char:5
+     Invoke-RestMethod -Method Post -Uri "$refreshEndpoint" -Body (Con ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
 

 

 

 

 

I checked again and again but couldn't develop any progress. Please help 

  • HassanRizvi,

    Can you try below powershell script once. this is working fine for me

     

     

    "<< API >>"
    > while ($true) {
    >>     $payload = @(
    >>         @{
    >>             "Date" = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")  # Current UTC DateTime
    >>             "Name" = "AAAAA555555"
    >>             "Marks" = Get-Random -Minimum 0 -Maximum 100  # Generate a random number
    >>         }
    >>     )
    >>
    >>     $headers = @{
    >>         "Content-Type" = "application/json"
    >>     }
    >>
    >>     # Send data to Power BI
    >>     Invoke-RestMethod -Method Post -Uri "$endpoint" -Headers $headers -Body (ConvertTo-Json $payload -Depth 3)
    >>
    >>     Write-Host "Data sent at $(Get-Date)"
    >>
    >>     Start-Sleep -Seconds 10  # Wait for 10 seconds before the next update
    >> }

     

     

    PFB screenshot for same:

     

     

     

     

    '

    Thanks,

    Prashanth Are

    MS Fabric community support

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

2 Replies

  • v-prasare's avatar
    v-prasare
    Community Support

    HassanRizvi,

    Can you try below powershell script once. this is working fine for me

     

     

    "<< API >>"
    > while ($true) {
    >>     $payload = @(
    >>         @{
    >>             "Date" = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")  # Current UTC DateTime
    >>             "Name" = "AAAAA555555"
    >>             "Marks" = Get-Random -Minimum 0 -Maximum 100  # Generate a random number
    >>         }
    >>     )
    >>
    >>     $headers = @{
    >>         "Content-Type" = "application/json"
    >>     }
    >>
    >>     # Send data to Power BI
    >>     Invoke-RestMethod -Method Post -Uri "$endpoint" -Headers $headers -Body (ConvertTo-Json $payload -Depth 3)
    >>
    >>     Write-Host "Data sent at $(Get-Date)"
    >>
    >>     Start-Sleep -Seconds 10  # Wait for 10 seconds before the next update
    >> }

     

     

    PFB screenshot for same:

     

     

     

     

    '

    Thanks,

    Prashanth Are

    MS Fabric community support

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

    • HassanRizvi's avatar
      HassanRizvi
      New Member

      Hi Prashanth,

      It is really helpful for me, can you please guide me how can I make the Power Bi report to update on its own.

      with your code, I have to click on the refresh button to update the graph.

       

      Thanks for this immense support