为什么条件语句不能正常工作?



我有一些报告要循环使用,获取连接ID(用户ID(,并将这些报告与用户名一起列出。

报告可能有以下情况:

  • 没有数据源
  • 1个数据源(1个用户ID(
  • 超过1个DataSources(因此超过1个用户id(

下面的脚本完成了这项工作,但是,由于某种原因,只有1个数据源的报告似乎在else ... No Connection ID's found!语句中执行。考虑到至少有1个数据源,情况不应该是这样,所以他们应该通过这个if ($DataSourceValue.DataModelDataSource.count -gt 0)语句!

以下是当前输出与预期输出的脚本:

$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 old 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 { 
}

电流输出:

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!

预期输出:

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!

请注意,第4个报告没有连接ID,因此状态为No Connection ID's found!是合理的。但是报告1和2有1个ID,所以状态应该是Retrieved!,而不是当前显示的No Connection ID's found!。对于第三个报告,它似乎是因为它有多个DataSource,条件语句正在正确应用。。。


编辑:

$DataSourceValue.DataModelDataSource.GetType()

收益率:

对于报告1&2:

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

对于报告3:

True     True     Object[]                                 System.Array

编辑2:

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 :

我的意思不是这个答案,但你能通过替换这部分代码来测试你的脚本吗:

$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)
{
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!"
}
}

为此:

$DataSourceValue = $DataSourceQueue.Dequeue().Value

if (-not $DataSourceValue)
{ 
write-host "$loopCount | $reportPath | No DataSource connection exists for this Report!";
}
else
{
if ($DataSourceValue.DataModelDataSource.Username)
{
foreach($i in $DataSourceValue.DataModelDataSource.Username)
{                
write-host "$loopCount | $reportPath | $i | Retrieved!"
}
}
else
{
write-host "$loopCount | $reportPath | $($DataSourceValue.DataModelDataSource.Username) | No Connection ID's found!"
}
}

编辑:在这里完成新的脚本,让我们看看这是否有效。

$ErrorActionPreference = 'Stop'
$loopCount = 1
$hash = @{
UseDefaultCredentials = $true
}
try
{
$hash.Uri = "$webPortalURL/api/v2.0/PowerBIReports"
$PBI_Reports_Array = (Invoke-RestMethod @hash).Value.Path
foreach ($reportPath in $PBI_Reports_Array)
{
$hash.Uri = "$webPortalURL/api/v2.0/PowerBIReports(path='$reportPath')/DataSources"

try
{
$DataSourceQueue = (Invoke-RestMethod @hash).Value
}
catch
{
Write-Warning ("$loopCount | ERROR! Can't connect to {0}" -f $hash.uri)
continue
}
foreach ($value in $DataSourceQueue)
{
if (-not $value.DataModelDataSource)
{
write-host "$loopCount | $reportPath | No DataModelDataSource"
continue
}
foreach($i in $value.DataModelDataSource)
{
if($i.Username)
{
$i.Username | foreach-object {
write-host "$loopCount | $reportPath | $_ | Retrieved!"
}
}
else
{
write-host "$loopCount | $reportPath | DataModelDataSource Found but No Usernames"
}
}
}
$loopCount++
}
}
catch
{
Write-Warning ("ERROR! Can't connect to {0}" -f $hash.uri)
}

最新更新