Why does the Array stops storing upon 404 error?

Welcome Forums General PowerShell Q&A Why does the Array stops storing upon 404 error?

Viewing 4 reply threads
  • Author
    Posts
    • #221640
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      I have been working on the following code that supposedly retrieves all powerbi reports from the server, checks if they have refresh plans, if they dont, it outputs “No refresh pans exist..”, and if it does have it, then it outputs refreshplan info like description.

      $webPortalURL = "https://server-pbi.domain.com/reports"
      
      $PBI_Reports_Array = @()
      $PBI_Reports_Array = $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports"))
      
      $loopCount = 0 
      $refreshPlanArray = @()
      
      foreach ($reportPath in $PBI_Reports_Array.value.path) {
      
              $refreshPlanArray += $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports(path='" + $reportPath + "')/CacheRefreshPlans")); 
      
              write-host "$($refreshPlanArray[$loopCount])" -foregroundcolor magenta; #testing output here to debug
      
              if ([string]::IsNullOrEmpty($($refreshPlanArray[$loopCount].value))) { 
                  write-host "$loopCount | $reportPath | No Refresh Plan Exists for this report!"; 
              }
              else {
                  write-host "$loopCount | $reportPath | $($refreshPlanArray[$loopCount].value.Description) | $($refreshPlanArray[$loopCount].value.ScheduleDescription)" -foregroundcolor magenta;
              }
      
          $loopCount++;
      }

      i am running into a weird bug. so i have 2 servers/portals, on one of the servers/portals, when i run this script, it retrieves all reports and does exactly what i am expecting it do as described above. when i thought i finished developing the script, i tested it on production portal/server and it wasnt working as expected! i debugged for many hours until i think i found whats happening, but idk what to do about it:

      basically, the reason why it worked on one server/portal but not the other is because the nonproduction portal/server didnt have this error:

      Invoke-RestMethod : The remote server returned an error: (404) Not Found.
      

      apparently, before that error happened on the production server/portal where this failed, this line write-host "$($refreshPlanArray[$loopCount])" i added for debugging purposes was printing the following odata contexts up until the error happened!

      @{@odata.context=https://server-pbi.domain.com/reports/api/v2.0/$metadata#CacheRefreshPlans; value=System.Object[]}
      

      then when the error occurred after iteration 4, it stopped printing the odata!

      why is that?

      o

    • #221649
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

      Thanks

      https://stackoverflow.com/questions/61380231/why-does-the-array-stop-storing-when-an-error-happens

    • #221667
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

      Thanks

      https://stackoverflow.com/questions/61380231/why-does-the-array-stop-storing-when-an-error-happens

      Thanks for adding the link. I was planning to link it whichever site I got the answer on first, or probably delete the other post on SO if I got an answer here.

    • #221859
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      There is no error handling. Also, use objects to capture data, not Write-Host as string. Using an object allows you to search for any NULL values to identify what did not work for investigation or reporting:

      $webPortalURL = "https://server-pbi.domain.com/reports"
      
      
      try {
          $reportParams = @{
              UseDefaultCredentials = $true
              Uri                   = $webPortalURL + "/api/v2.0/PowerBIReports"
              ErrorAction           = 'Stop'
          }
      
          $responseReports = Invoke-RestMethod @reportParams
      
          $results = foreach ($reportPath in $responseReports.value.path) {
             
              try {
                      $refreshParams = @{
                          UseDefaultCredentials = $true
                          Uri                   = $webPortalURL + "/api/v2.0/PowerBIReports(path='" + $reportPath + "')/CacheRefreshPlans"
                          ErrorAction           = 'Stop'
                      }
      
                      $responsePath = Invoke-RestMethod @refreshParams 
      
                      [PSCustomObject]@{
                          ReportPath          = $reportPath
                          Description         = $responsePath.Value.Description
                          ScheduleDescription = $responsePath.value.ScheduleDescription
                      }
      
              }
              catch {
                  [PSCustomObject]@{
                      ReportPath          = $reportPath
                      Description         = $null
                      ScheduleDescription = $null
                  }
              }
          }
      }
      catch {
          'Trouble getting stuff from {0}. {1}' -f $reportParams.Uri, $_
      }
      
      $results
      
    • #221922
      Participant
      Topics: 39
      Replies: 108
      Points: 620
      Rank: Major Contributor

      thank you! this works! but this part isnt doing/printing anything,

      'Trouble getting stuff from {0}. {1}' -f $reportParams.Uri, $_

      even though i know that there is definitely at least 1 error after iteration 4 per the screenshot i have.

      also, the splatting definitely helps. as far as write-host, i was going to use pscutomobject in my official code, i was just using write-host cause its quick for debugging purposes. after the code works, i go back and improvise the code quality. thats my habit 🙂

      btw, the reason why my code wasnt working is because of the $loopCount.

      $loopCount is the culprit! it has to be inside a try, or the “good” part of the code, otherwise, the array indexing gets messed up with the 404 NULL value

      This is the correct adjustment to my code:

      $webPortalURL = "https://server-pbi.domain.com/reports"
      
      $PBI_Reports_Array = @()
      $PBI_Reports_Array = $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports"))
      
      $loopCount = 0 
      $refreshPlanArray = @()
      
      foreach ($reportPath in $PBI_Reports_Array.value.path) {
          try {
              $refreshPlanArray += $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports(path='" + $reportPath + "')/CacheRefreshPlans"));
      
              if ([string]::IsNullOrEmpty($($refreshPlanArray[$loopCount].value))) { 
                  write-host "$loopCount | $reportPath | No Refresh Plan Exists for this report!"; 
              }
              else {
                  write-host "$loopCount | $reportPath | $($refreshPlanArray[$loopCount].value.Description) | $($refreshPlanArray[$loopCount].value.ScheduleDescription)" -foregroundcolor magenta;
              }
      
              $loopCount++;
          }
          catch {
      
          }
      }
Viewing 4 reply threads
  • You must be logged in to reply to this topic.