Skip to content

Report: name failed collectors, count consolidated subscriptions - #51

Merged
stefoy merged 11 commits into
awslabs:mainfrom
pmorgenrood:upstream-pr/collector-incomplete-banner
Nov 25, 2026
Merged

stefoy merged 11 commits into
awslabs:mainfrom
pmorgenrood:upstream-pr/collector-incomplete-banner

Conversation

@pmorgenrood

@pmorgenrood pmorgenrood commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Part of #22: the report now says which resource types are missing because their collector failed, and a direct run that
lost a collector exits 4 instead of 0. The fourth commit fixes the same header for a subscription with no resources,
which showed no number after Total Resources: and (unknown) as the subscription. The latest update makes a report that
covers several subscriptions say how many it covers (see Update below).

Problem

When a collector under Services/ throws, ResourceInventory.ps1 catches it, records it in $Global:CollectorFailures and
carries on with the next collector. The failed resource type is written to the Inventory JSON as an empty array, so
Extension/Summary.ps1 drops its section from the HTML report, the same as for a subscription that has none of that type. The
run exits 0. The person who reads the report has no way to tell that a resource type is missing.

Reproduced on main with a direct run on a test subscription where the VMSS collector fails (the date bug that #39 fixes):

./ResourceInventory.ps1 -SubscriptionID <sub-id> -SkipMetrics -SkipConsumption -OutputDirectory <out>
  • In the middle of the run:
    Collector FAILED: VMSS: Cannot convert null to type "System.DateTime". and
    The rest of the inventory will continue, but the 'VMSS' resource type is MISSING from this report ...
  • At the end of the run: HTML report written: <out>/ResourcesReport_<timestamp>.html,
    Total resources: <n> across 9 service type(s), exit code 0. Nothing at the end repeats the failure.
  • The HTML report: no mention of VMSS, FAILED, MISSING or incomplete.

The Diagnostics log in the zip already names the collector on main
(Collector failures: 1 followed by [sub <guid>] VMSS: Cannot convert null to type "System.DateTime".), and
Run-AllSubscriptions.ps1 already lists collector failures in its final summary. What was missing is the per-subscription
HTML report and the end of that report's console output.

The same header had a second gap. For a subscription with no resource that the collectors report, for example a new or
emptied subscription, the header of the HTML report, in the output folder and in the copy in the zip, read:

<div><b>Subscription:</b> (unknown)</div>
<div><b>Total Resources:</b> </div>

and the console line read Total resources: across 0 service type(s). Extension/Summary.ps1 sums the per-type record
counts with Measure-Object, which gives no sum for no input, and takes the subscription name from the first record, of
which there is none. ResourceInventory.ps1 does not pass -SubscriptionName. Reproduced on main and on the third commit
of this branch, offline, with the real ResourceInventory.ps1 (default metrics and consumption phases) and stand-ins for the
Azure CLI and Az calls answering for a subscription with no resources.

Change

  • ResourceInventory.ps1: the collector catch block also adds the failure to $script:CollectorFailuresThisRun, which is
    set to @() next to $script:PhaseTimings at the start of each run, and ProcessSummary passes it to Summary.ps1 as
    -CollectorFailures. A separate list is needed because $Global:CollectorFailures is a running total across every
    subscription of a Run-AllSubscriptions.ps1 run (sequential and -ParallelStreams both call ResourceInventory.ps1 with &
    once per subscription), so passing it would show one subscription's failures in the next subscription's report.

  • Extension/Summary.ps1: new optional -CollectorFailures parameter (objects with a Module property, the shape
    ResourceInventory.ps1 already records). When it holds at least one record, the report shows, under the privacy banner, in the
    existing coverage-banner style:

    Incomplete collection: 1 resource type(s) could not be collected and are MISSING from this report - not empty because
    there are none, but because the collector errored: VMSS. See the Diagnostics log in this bundle for the underlying error,
    then re-run.

    The header's Service Types value gets (collection incomplete, see below), and the summary's console output ends with
    Collection INCOMPLETE: 1 resource type(s) missing because the collector errored: VMSS. Each type is listed once, sorted,
    and HTML-escaped; a record with no Module is shown as (unnamed collector) so it cannot hide the banner. With no failures,
    or when Summary.ps1 is run on its own without the parameter, the report is unchanged.

  • Exit code (second commit). A direct ResourceInventory.ps1 run, meaning one without -RunAllSubs, now exits 4 when
    this run recorded at least one collector failure and its zip was written. Before, it exited 0. 4 is the code
    Run-AllSubscriptions.ps1 already uses for "one or more collectors failed". I decided this. There is no switch to keep
    0. Every other direct run ends as before: 0 with no collector failure, 1 from the pre-flight checks. A run that
    wrote no zip keeps its old exit code, because 4 promises a written report.

  • Under -RunAllSubs the new exit is skipped. All three places the wrapper calls ResourceInventory.ps1 (the sequential
    loop and the one-subscription fallback of -ParallelStreams in Run-AllSubscriptions.ps1, and the stream worker in
    Run-AllSubscriptions.Stream.ps1) treat any non-zero exit as a failed subscription, so a 4 there would turn a
    completed subscription into a failed one. The wrapper's per-subscription status, its summary and its own exit codes are
    unchanged.

  • README.md: a table of the direct run's exit codes (0, 1, 4) under the wrapper's table. The wrapper's rows are
    unchanged.

  • Empty-subscription header (fourth commit). Extension/Summary.ps1: with no records the total is 0, in the header and
    in the console line. ResourceInventory.ps1: a run of one subscription (-SubscriptionID) without -Obfuscate passes
    that subscription's name from the subscription list to Summary.ps1 as -SubscriptionName, the way the tenant id is
    passed. For a subscription with records this is the name the first record already carried. Under -Obfuscate, and for a
    run over several subscriptions (no -SubscriptionID), the name is still taken from the first record, so an obfuscated
    report keeps its masked name and an empty obfuscated report shows (unknown) rather than the real name. No column, exit
    code or stop changes.

Testing

  • New Tests/CollectorFailureBanner.Tests.ps1 (8 tests). It runs the real Summary.ps1 on a small generated inventory and
    checks the HTML and the console output: one failure, several failures (deduplicated and sorted), a record with a blank
    Module, an HTML-escaped name, the console line at the end, and no banner or line when the parameter is omitted, empty or holds
    only $null. On main 5 fail and 3 pass (the three no-failure cases); on this branch all 8 pass.
  • New Tests/DirectRunExitCode.Tests.ps1 (9 tests). It copies the script to a temporary folder, adds small collectors there
    (one returns a record, the others throw) and runs the real ResourceInventory.ps1 in a child pwsh with
    -SkipMetrics -SkipConsumption and offline stand-ins for the Azure CLI and Az calls. It checks: one failing collector ->
    report and zip written, banner names it, exit 4; no failure -> exit 0; -Service matching nothing -> still exit 1;
    five failing collectors in a row (no zip) -> not 4; under -RunAllSubs with a failing collector -> exit 0, and each of
    the three wrapper call sites still has the same $LASTEXITCODE check, which does not throw for that 0. On main 7 pass
    and 2 fail (the banner and Expected 4, but got 0.); on the first commit of this branch 8 pass and only the exit 4 test
    fails; from the second commit on, all 9 pass. The harness also stands in for the up-front read-access check that Check read access and usage values up front for every subscription #38 adds (the
    token, role assignment, role definition and permissions requests answer as for a Reader assignment on the subscription, and
    any other Resource Manager call fails), so the test passes with or without Check read access and usage values up front for every subscription #38.
  • New Tests/EmptySubscriptionReport.Tests.ps1 (8 tests). It copies the script to a temporary folder and runs the real
    ResourceInventory.ps1 in a child pwsh with the default metrics and consumption phases and offline stand-ins for the
    Azure CLI and Az calls: for a subscription with no resources, without and with -Obfuscate, and for one with a single disk.
    It reads the header of the report in the output folder and of the copy in the zip, and the console line. On main and on
    the third commit of this branch 2 pass and 6 fail (Expected: '0' / But was: '' for Total Resources,
    Expected: 'Demo Subscription' / But was: '(unknown)' for the subscription); on the fourth commit all 8 pass. The
    obfuscated case checks that the real subscription name appears nowhere in either copy of the report. The one-disk report
    is the same before and after the fourth commit apart from its Generated: time.
  • Full Pester suite: main 269 passed / 162 failed / 22 skipped; first commit 277 / 162 / 22; second and third commits 286 / 162 / 22;
    fourth commit 294 / 162 / 22, the same failing tests each time. PSScriptAnalyzer with the repository settings and the
    workflow's paths: nothing new; on the third and fourth commits its only result is the same existing warning in
    Tests/RecoveryMerge.Tests.ps1.
  • Live, the direct run above on the same test subscription with this branch (read-only, metrics and consumption skipped): the
    HTML report and the copy in the zip both show the banner naming VMSS, Service Types reads
    9 (collection incomplete, see below), the summary's console output ends with the Collection INCOMPLETE line, and the
    exit code is 4.
  • Live, Run-AllSubscriptions.ps1 -SkipMetrics -SkipConsumption over three test subscriptions (read-only; the wrapper chose
    -ParallelStreams 2, so the stream worker made the calls), once on the first commit and once on the second: the same
    result both times. All 3 subscriptions processed, 3 zip(s) match 3 successful sub(s), 0 failed, the VMSS failure listed
    under Collector Failures, the FAILED (collectors) banner, and wrapper exit code 4.
  • The fourth commit was checked offline only, with the test above; it was not run on Azure. Run-AllSubscriptions.ps1
    calls the script with -SubscriptionID for each subscription, so its per-subscription reports take the same code path;
    a wrapper run was not made for this commit.
  • git merge-tree against the current head of each other open PR: Check read access and usage values up front for every subscription #38 conflicts in ResourceInventory.ps1 and README.md,
    because both add lines at the end of the script and below the wrapper's exit-code table. Whichever merges second keeps both
    sides, with this PR's exit 4 as the script's last statement. The fourth commit's lines merge outside those conflicts.
    Exit 2 from a direct run that wrote no report #52 conflicts in README.md, where both add a section for the direct run's exit codes under the wrapper's table. The
    other open PRs merge cleanly. Mask a parent's name inside the obfuscated id of a child resource #53 also changes Extension/Summary.ps1 (the obfuscation token pattern) and merges cleanly.

Update: how many subscriptions a consolidated report covers

A direct run without -SubscriptionID that covers more than one subscription now shows
N subscriptions (consolidated) in the report header, instead of the name of whichever subscription came first. Only
subscriptions whose state is Enabled are counted, the same filter the run uses to choose what it inventories. A count
reveals nothing, so the same header is used under -Obfuscate. A run with -SubscriptionID, or of one subscription, is
unchanged. Only ResourceInventory.ps1 changes; no inventory, consumption or metrics field changes.

Tested offline with the real script: new Tests/MultiSubscriptionHeader.Tests.ps1 runs it in a child pwsh with stand-ins
for the Azure calls, for two subscriptions with and without -SubscriptionID, a disabled subscription, -Obfuscate, and
en-US and tr-TR. On the previous head of this PR 6 fail (the header and the disabled subscription); on this head all pass.
The tr-TR case of the subscription name is skipped: on main a tr-TR run loses the record's subscription name before this
code runs, which #66 fixes. Not run on Azure, and not run on Windows.

How to test

Quick check (no Azure)

Needs PowerShell 7 and Pester 5 (Install-Module Pester -MinimumVersion 5.0 -Scope CurrentUser). The tests write only to a
temporary folder.

From the repository root:

git fetch origin main pull/51/head:pr-collector-banner

# main's code with this PR's test files
git worktree add ../rda-main origin/main
git -C ../rda-main checkout pr-collector-banner -- Tests/CollectorFailureBanner.Tests.ps1 Tests/DirectRunExitCode.Tests.ps1 Tests/EmptySubscriptionReport.Tests.ps1
(cd ../rda-main && pwsh -NoProfile -Command "Invoke-Pester -Path ./Tests/CollectorFailureBanner.Tests.ps1,./Tests/DirectRunExitCode.Tests.ps1,./Tests/EmptySubscriptionReport.Tests.ps1 -Output Detailed")
git worktree remove --force ../rda-main

# this PR
git checkout pr-collector-banner
pwsh -NoProfile -Command "Invoke-Pester -Path ./Tests/CollectorFailureBanner.Tests.ps1,./Tests/DirectRunExitCode.Tests.ps1,./Tests/EmptySubscriptionReport.Tests.ps1 -Output Detailed"
  • On main: Tests Passed: 12, Failed: 13, for example
    Expected regular expression '<b>Incomplete collection:</b> 1 resource type\(s\) could not be collected and are MISSING from this report' to match '<!DOCTYPE html>,
    for the console line Expected: ' Collection INCOMPLETE: 2 resource type(s) missing because ...' /
    But was: ' Privacy posture: identifiable (contains real names; treat ...', for the exit code
    Expected 4, but got 0., and for the empty subscription's header Expected: '0' / But was: '' and
    Expected: 'Demo Subscription' / But was: '(unknown)'.
  • On this branch: Tests Passed: 25, Failed: 0.

(Of these steps, the two Invoke-Pester commands were run as written, on local exports of main with the three test files
and of this branch; the git fetch of the PR head and the worktree steps were not.)

Reproduce on Azure (read-only)

Needs a test subscription where a collector fails. Until #39 is merged, one empty Flexible scale set is enough: #39's
"How to test" shows how to create one at no cost (az vmss create ... --orchestration-mode Flexible with no image). Needs
PowerShell 7 with the Az modules signed in (Connect-AzAccount) and the Azure CLI signed in (az login). The run only reads
from Azure.

pwsh -NoProfile -File ./ResourceInventory.ps1 -SubscriptionID <sub-id> -SkipMetrics -SkipConsumption -OutputDirectory <out> < /dev/null
echo "exit code: $?"
  • On main: the Collector FAILED: VMSS line in the middle of the run, nothing about it in the HTML report or after
    Total resources:, and exit code: 0.
  • On this branch: the same mid-run lines, plus the Incomplete collection: banner in <out>/ResourcesReport_<timestamp>.html,
    Collection INCOMPLETE: 1 resource type(s) missing because the collector errored: VMSS after the
    Privacy posture: line, and exit code: 4.

Delete the scale set and its resource group afterwards.

Not changed

  • Run-AllSubscriptions.ps1 and Run-AllSubscriptions.Stream.ps1: no edits. Under -RunAllSubs the inner script ends as
    before, and the wrapper's exit codes (4 for collector failures) are unchanged.
  • The collector circuit breaker. When 5 collectors fail in a row, collection still ends before the inventory is written, so
    no report or zip is produced, and the run ends with 0 as before this PR. The new 4 does not fire there because no zip
    was written. Giving that case its own exit code is a separate change.
  • The Diagnostics log, which already names each failed collector.
  • ResourceInventory.ps1 still prints its packaging and timing lines (Compressing Resources Output, Execution Time, ...)
    after the summary, so the Collection INCOMPLETE line ends the summary block, not the whole console output.
  • From Collector errors are silently swallowed by global SilentlyContinue + missing try/catch #22's suggested steps, this covers part of step 2: the report now names the collectors that failed. It does not add a
    full status list of every collector with its resource count. Step 1 (a try/catch inside each collector, with the inner
    exception and the record being processed) and step 3 (replacing the global $ErrorActionPreference = "silentlycontinue" with
    per-call handling) are not part of this change.

Peter-John Morgenrood added 3 commits October 27, 2026 20:41
When a collector throws, its resource type is written as an empty array
and dropped from the HTML report, which then reads the same as a
subscription that has none of that type. The run carries on and exits 0,
and the only mention of the failure is a console line in the middle of
the run.

ResourceInventory.ps1 now keeps this run's collector failures in a
script-scoped list, because $Global:CollectorFailures is a running total
across every subscription of a Run-AllSubscriptions run, and passes it
to Extension/Summary.ps1 as -CollectorFailures. Summary.ps1 shows an
"Incomplete collection" banner naming the missing resource types, adds a
note next to Service Types, and ends its console output with a
"Collection INCOMPLETE" line. Nothing changes when no collector failed.
Exit codes are unchanged.

Tests/CollectorFailureBanner.Tests.ps1 runs the real Summary.ps1 on a
small inventory with and without failures.
A direct ResourceInventory.ps1 run in which one or more collectors failed
writes the full report and zip, and used to exit 0. It now exits 4, the
code Run-AllSubscriptions.ps1 already uses for collector failures, once the
zip exists. A run that wrote no zip keeps its exit code.

Under -RunAllSubs nothing changes: every wrapper call site treats a
non-zero exit as a failed subscription, and the wrapper reports collector
failures through its own exit code.

README documents the direct run's exit codes next to the wrapper's table.
Tests/DirectRunExitCode.Tests.ps1 runs the real script with offline
stand-ins for Azure.
A direct run that checks read access up front through Resource Manager
before any collector runs found no answer for those calls in the harness,
so every direct run stopped with exit 1 before collection. The harness now
answers the token, role assignment, role definition and permissions requests
as for a Reader assignment on the subscription, so the subscription is
readable and the run reaches the collectors. Any other Resource Manager call
fails in the harness. A run without that check makes none of these calls,
so the test passes with or without it.
… report

The report of a subscription with no resources read 'Total Resources:'
with nothing after it, and 'Subscription: (unknown)', in the HTML in the
output folder and in the copy in the zip.

Summary.ps1 summed the per-type record counts with Measure-Object, which
gives no sum for no input; the total is now 0 then, in the header and in
the console line. Summary.ps1 also took the subscription name from the
first record, and ResourceInventory.ps1 did not pass -SubscriptionName.
A run of one subscription now passes that subscription's name, the way the
tenant id is passed. Under -Obfuscate, and for a run of several
subscriptions, the name is still taken from the first record, so an
obfuscated report keeps its masked name and an empty obfuscated report
shows '(unknown)' rather than the real name.
pmorgenrood pushed a commit to pmorgenrood/resource-discovery-for-azure that referenced this pull request Oct 30, 2026
…om a direct run when one failed

# Conflicts:
#	README.md
#	ResourceInventory.ps1
pmorgenrood pushed a commit to pmorgenrood/resource-discovery-for-azure that referenced this pull request Oct 30, 2026
…wslabs#59/awslabs#61/awslabs#64/awslabs#53/awslabs#56): run the offline harnesses on the Az PowerShell path

The child-process harnesses of these PRs answered sign-in, the subscription
list and Resource Graph through an 'az' stand-in. awslabs#66 drops the Azure CLI, so
that stand-in is never called. Tests/Helpers/AzPsStandIns.ps1 now gives every
harness Get-AzSubscription, Get-AzAccessToken -TenantId, a signed-in context
and Resource Graph paging through Invoke-AzRestMethod with skip tokens, and
answers each page from the harness's own graph stand-in (renamed
Invoke-StandInGraph). Discovery queries go through Invoke-DiscoveryGraphQuery
(awslabs#59), so the -HideRawOutput check reads those calls. awslabs#59's unparsable page is
now a cut-off page (an empty property name is kept since awslabs#66), and awslabs#52's setup
stop is shown with a missing Az module instead of a missing Azure CLI.
Peter-John Morgenrood added 6 commits November 9, 2026 10:13
A direct run without -SubscriptionID inventories every subscription the
sign-in lists into one report, but its header read 'Subscription: <name>'
with one subscription's name: Summary.ps1 took it from the first record.
For a run over more than one subscription the header now reads
'N subscriptions (consolidated)'. A count says nothing that -Obfuscate
would mask, so it is used under -Obfuscate too. A run with -SubscriptionID,
or of one subscription, is unchanged.

The new test runs the real script against offline stand-ins for the Azure
CLI and the Az cmdlets: two subscriptions without -SubscriptionID (with and
without -Obfuscate), two with -SubscriptionID, and one without, under en-US
and tr-TR (the one-subscription case under en-US only: on this base a tr-TR
run loses the record's subscription name before this code is reached).
The header test carried its own copy of the offline stand-ins for the Azure
CLI and the Az cmdlets. It now runs the real script through the shared
Tests/Helpers/InnerRunHarness.ps1, added here unchanged, so the stand-ins
live in one place. The cases and what they check are the same.
The 'N subscriptions (consolidated)' header counted every entry of the
subscription list, so an entry whose state is not Enabled would be counted
as covered. The count now keeps only entries whose state is exactly
'Enabled', the same filter 'az account list' applies by default when it
builds that list. Under -TenantID the list is already narrowed to that
tenant before the count, so another tenant's subscription is not counted.

The header test gains three runs through a small local stand-in that adds
entries to the subscription list only: a Disabled and a Warned entry next
to two Enabled ones, a Disabled entry next to one Enabled one, and another
tenant's subscription under -TenantID.
The analyzer's brace-placement rule flagged three case tables in the header test. Only the layout changes.
@pmorgenrood pmorgenrood changed the title Name failed collectors in the report, and exit 4 from a direct run when one failed Report: name failed collectors, count consolidated subscriptions Nov 12, 2026
pmorgenrood pushed a commit to pmorgenrood/resource-discovery-for-azure that referenced this pull request Nov 17, 2026
@stefoy
stefoy merged commit 66e045a into awslabs:main Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants