为什么发生错误时数组停止存储?
Why does the array stop storing when an error happens?
我一直在研究以下代码,据说可以从服务器检索所有 powerbi 报告,检查它们是否有刷新计划,如果没有,它输出 "No refresh pans exist..",如果有,那么它输出刷新计划信息,如描述。
$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++;
}
我 运行遇到了一个奇怪的错误。所以我有 2 servers/portals,在其中一个 servers/portals 上,当我 运行 这个脚本时,它会检索所有报告并按照我期望的方式执行上述操作。
当我以为我完成了脚本开发时,我在生产环境 portal/server 上对其进行了测试,但它并没有按预期工作!
我调试了好几个小时,直到我想我发现发生了什么,但我不知道该怎么办:
基本上,它对一个 server/portal 但对另一个不起作用的原因是因为非生产 portal/server 没有这个错误:
Invoke-RestMethod : The remote server returned an error: (404) Not Found.
显然,在错误发生在生产 server/portal 失败之前,我为调试目的添加的这一行 write-host "$($refreshPlanArray[$loopCount])"
打印了以下 odata context
s 直到错误发生!
@{@odata.context=https://server-pbi.domain.com/reports/api/v2.0/$metadata#CacheRefreshPlans; value=System.Object[]}
然后当第4次迭代后出现错误时,它停止打印odata!
这是为什么?
我想通了!
$loopCount
是罪魁祸首!它必须在 try 中,或者代码的 "good" 部分,否则,数组索引会被 404 NULL 值
弄乱
这是正确的代码:
$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 {
}
}
我一直在研究以下代码,据说可以从服务器检索所有 powerbi 报告,检查它们是否有刷新计划,如果没有,它输出 "No refresh pans exist..",如果有,那么它输出刷新计划信息,如描述。
$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++;
}
我 运行遇到了一个奇怪的错误。所以我有 2 servers/portals,在其中一个 servers/portals 上,当我 运行 这个脚本时,它会检索所有报告并按照我期望的方式执行上述操作。 当我以为我完成了脚本开发时,我在生产环境 portal/server 上对其进行了测试,但它并没有按预期工作! 我调试了好几个小时,直到我想我发现发生了什么,但我不知道该怎么办:
基本上,它对一个 server/portal 但对另一个不起作用的原因是因为非生产 portal/server 没有这个错误:
Invoke-RestMethod : The remote server returned an error: (404) Not Found.
显然,在错误发生在生产 server/portal 失败之前,我为调试目的添加的这一行 write-host "$($refreshPlanArray[$loopCount])"
打印了以下 odata context
s 直到错误发生!
@{@odata.context=https://server-pbi.domain.com/reports/api/v2.0/$metadata#CacheRefreshPlans; value=System.Object[]}
然后当第4次迭代后出现错误时,它停止打印odata!
这是为什么?
我想通了!
$loopCount
是罪魁祸首!它必须在 try 中,或者代码的 "good" 部分,否则,数组索引会被 404 NULL 值
这是正确的代码:
$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 {
}
}