2021年5月3日星期一

Why isn't the conditional statement working right?

I've got some Reports that Im trying to loop through, and fetch the connection ID's (User ID's) and list those reports along with the usernames.

A report can have the following scenarios:

  • No DataSources
  • 1 DataSource (1 User ID)
  • More than 1 DataSources (Therefore more than 1 user id's)

The following script does the job, however, for some reason, reports with only 1 datasource seem to be getting executed in the else ... No Connection ID's found! statement. That shouldn't be the case considering there is AT LEAST 1 Datasource, so they should be going through this if ($DataSourceValue.DataModelDataSource.count -gt 0) statement instead!

Below is script accompanied by current output vs expected output:

$loopCount = 1  $PBI_Reports_Array = @()    $DataSourceQueue = New-Object System.Collections.Queue    try {      $PBI_Reports_Array = $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports"))         foreach ($reportPath in $PBI_Reports_Array.value.path) {          try {              foreach ($DataSource in $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports(path='" + $reportPath + "')/DataSources")))              {                  $DataSourceQueue.Enqueue($DataSource)              }                            while($DataSourceQueue.count)              {                     $DataSourceValue = $($DataSourceQueue.Dequeue()).value                                    if ([string]::IsNullOrEmpty($($DataSourceValue))) {                       write-host "$loopCount | $reportPath | No DataSource connection exists for this Report!";                  }                  else {                      if ($DataSourceValue.DataModelDataSource.count -gt 0) #if there is at least 1+ DataSources and usernames...                      {                          #because there is more than 1 DataSources, that means there's also more than 1 connection ID and more than 1 connection ID's                           #to loop through both of those variables, foreach loop would not be suitable for more than 1 variable,                          #so we use good ol' for loop                                                    0..($DataSourceValue.DataModelDataSource.length-1) | ForEach-Object {                                                                write-host "$loopCount | $reportPath | $($DataSourceValue.DataModelDataSource[$_].Username) | Retrieved!"                          }                      }                      else                      {                          write-host "$loopCount | $reportPath | $($DataSourceValue.DataModelDataSource.Username) | No Connection ID's found!"                      }                  }              }              #$loopCount++          }          catch {              write-host "$loopCount | ERROR! $($error[0])`r`n$($error[0].InvocationInfo.PositionMessage)`r`n$($error[0].ScriptStackTrace)"          }          $loopCount++      }  }  catch {   }  

Current output:

1 | /Cash Flow/CFG | SI_123456_P | No Connection ID's found!  2 | /CPUR/DQ Dashboards | gp_powerbi_cpur | No Connection ID's found!  3 | /ABC/DQ Dashboards/PreCost ABC DQ Dashboard | gp_powerbi_cpur | Retrieved!  3 | /ABC/DQ Dashboards/PreCost ABC DQ Dashboard | SI_123456_P | Retrieved!  4 | /Prototypes/ARCHIVE/dummy data |  | No Connection ID's found!  

Expected Output:

1 | /Cash Flow/CFG | SI_123456_P | Retrieved!  2 | /CPUR/DQ Dashboards | gp_powerbi_cpur | Retrieved!  3 | /ABC/DQ Dashboards/PreCost ABC DQ Dashboard | gp_powerbi_cpur | Retrieved!  3 | /ABC/DQ Dashboards/PreCost ABC DQ Dashboard | SI_123456_P | Retrieved!  4 | /Prototypes/ARCHIVE/dummy data |  | No Connection ID's found!  

Note the 4th report has no connection ID's, therefore makes sense that the status is No Connection ID's found!. but reports 1 and 2 have 1 ID, so the status should be Retrieved! not No Connection ID's found! as its currently displaying... For the 3rd report, it appears because it has more than 1 DataSource, the conditional statement is being applied correctly...


EDIT:

$DataSourceValue.DataModelDataSource.GetType()

yields:

for reports 1 & 2:

IsPublic IsSerial Name                                     BaseType  -------- -------- ----                                     --------  True     False    PSCustomObject                           System.Object  

for report 3:

True     True     Object[]                                 System.Array  
https://stackoverflow.com/questions/67377135/why-isnt-the-conditional-statement-working-right May 04, 2021 at 07:54AM

没有评论:

发表评论