title: Why isn't the conditional statement working right?
body: <p>I've got some Reports that I'm trying to loop through, and fetch the connection ID's (User ID's) and list those reports along with the usernames.</p>
  <p>A report can have the following scenarios:</p>
  <ul>
  <li>No DataSources</li>
  <li>1 DataSource (1 User ID)</li>
  <li>More than 1 DataSources (Therefore more than 1 user id's)</li>
  </ul>
  <p>The following script does the job, however, for some reason, reports with only 1 datasource seem to be getting executed in the <code>else ... No Connection ID's found!</code> statement. That shouldn't be the case considering there is AT LEAST 1 Datasource, so they should be going through this <code>if ($DataSourceValue.DataModelDataSource.count -gt 0)</code> statement instead!</p>
  <p>Below is script accompanied by current output vs expected output:</p>
  <pre><code>$loopCount = 1
  $PBI_Reports_Array = @()
  
  $DataSourceQueue = New-Object System.Collections.Queue
  
  try {
      $PBI_Reports_Array = $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + &quot;/api/v2.0/PowerBIReports&quot;)) 
  
      foreach ($reportPath in $PBI_Reports_Array.value.path) {
          try {
              foreach ($DataSource in $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + &quot;/api/v2.0/PowerBIReports(path='&quot; + $reportPath + &quot;')/DataSources&quot;)))
              {
                  $DataSourceQueue.Enqueue($DataSource)
              }
              
              while($DataSourceQueue.count)
              {   
                  $DataSourceValue = $($DataSourceQueue.Dequeue()).value
                  
                  if ([string]::IsNullOrEmpty($($DataSourceValue))) { 
                      write-host &quot;$loopCount | $reportPath | No DataSource connection exists for this Report!&quot;;
                  }
                  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 old for loop
                          
                          0..($DataSourceValue.DataModelDataSource.length-1) | ForEach-Object {
                                  
                              write-host &quot;$loopCount | $reportPath | $($DataSourceValue.DataModelDataSource[$_].Username) | Retrieved!&quot;
                          }
                      }
                      else
                      {
                          write-host &quot;$loopCount | $reportPath | $($DataSourceValue.DataModelDataSource.Username) | No Connection ID's found!&quot;
                      }
                  }
              }
              #$loopCount++
          }
          catch {
              write-host &quot;$loopCount | ERROR! $($error[0])`r`n$($error[0].InvocationInfo.PositionMessage)`r`n$($error[0].ScriptStackTrace)&quot;
          }
          $loopCount++
      }
  }
  catch { 
  }
  </code></pre>
  <p>Current output:</p>
  <pre><code>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!
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>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!
  </code></pre>
  <p>Note the 4th report has no connection ID's, therefore makes sense that the status is <code>No Connection ID's found!</code>. but reports 1 and 2 have 1 ID, so the status should be <code>Retrieved!</code> not <code>No Connection ID's found!</code> as it's currently displaying. For the 3rd report, it appears because it has more than 1 DataSource, the conditional statement is being applied correctly...</p>
  <hr />
  <p><em><strong>EDIT:</strong></em></p>
  <p><code>$DataSourceValue.DataModelDataSource.GetType()</code></p>
  <p>yields:</p>
  <p>for reports 1 &amp; 2:</p>
  <pre><code>IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     False    PSCustomObject                           System.Object
  </code></pre>
  <p>for report 3:</p>
  <pre><code>True     True     Object[]                                 System.Array
  </code></pre>
  <hr />
  <p><em><strong>EDIT 2:</strong></em></p>
  <pre><code>62 | /test/BMI vs Chocolate |  | Retrieved!
  
  Type                : Import
  Kind                : File
  AuthType            : Windows
  SupportedAuthTypes  : {Windows}
  Username            :
  Secret              :
  ModelConnectionName :
  
  62 | /test/BMI vs Chocolate |  | Retrieved!
  
  Type                : Import
  Kind                : File
  AuthType            : Windows
  SupportedAuthTypes  : {Windows}
  Username            :
  Secret              :
  ModelConnectionName :
  
  62 | /test/BMI vs Chocolate |  | Retrieved!
  
  Type                : Import
  Kind                : File
  AuthType            : Windows
  SupportedAuthTypes  : {Windows}
  Username            :
  Secret              :
  ModelConnectionName :
  
  63 | /VP/Complexity_PROD | gp_powerbi_vp | Retrieved!
  
  Id                                        : 12345-cvgfgh7-87964-e76ufhf5
  Name                                      :
  Description                               :
  Path                                      :
  Type                                      : DataSource
  Hidden                                    : False
  Size                                      : 0
  ModifiedBy                                : user1
  ModifiedDate                              : 2021-02-03T12:17:26.413-05:00
  CreatedBy                                 : SI_123456_P
  CreatedDate                               : 2021-02-03T12:13:42.523-05:00
  ParentFolderId                            :
  IsFavorite                                : False
  ContentType                               :
  Content                                   :
  IsEnabled                                 : True
  ConnectionString                          : db=maxisdb;driver={DataDirect 7.1 Greenplum Wire Protocol};em=1;host=gp.companyxyz.com;port=5432;vsc=0
  DataSourceType                            :
  IsOriginalConnectionStringExpressionBased : False
  IsConnectionStringOverridden              : False
  CredentialRetrieval                       : prompt
  IsReference                               : False
  DataSourceSubType                         : DataModel
  Roles                                     : {}
  CredentialsByUser                         :
  CredentialsInServer                       :
  DataModelDataSource                       : @{Type=Import; Kind=Odbc; AuthType=UsernamePassword; SupportedAuthTypes=System.Object[]; Username=gp_powerbi_vp; Secret=; ModelConnectionName=}
  
  Type                : Import
  Kind                : Odbc
  AuthType            : UsernamePassword
  SupportedAuthTypes  : {UsernamePassword, Anonymous, Windows}
  Username            : gp_powerbi_vp
  Secret              :
  ModelConnectionName :
  </code></pre>