Skip to content

[Bug]: PowerShell scripts fail with 'Count' property not found error when run in workflows #394

@katriendg

Description

@katriendg

Component

Scripts

Bug Description

Multiple PowerShell scripts in the repository were failing with the error:

PSScriptAnalyzer failed: The property 'Count' cannot be found on this object. Verify that the property exists.

This occurred in two workflow checks:

  1. PowerShell Lint (Invoke-PSScriptAnalyzer.ps1)
  2. Validate SHA Pinning Compliance (Test-DependencyPinning.ps1, Test-SHAStaleness.ps1)

The root cause was that scripts were directly accessing the .Count property on collections that could potentially be $null or empty results from helper functions like Get-ChangedFilesFromGit and Get-FilesRecursive.

Expected Behavior

PowerShell scripts should handle empty or null collection results gracefully without throwing property access errors. All collection operations should be safe and defensive.

Proposed Solution

Ensure all collection assignments and count checks wrap results in @() to guarantee they're always arrays:

Before:

$filesToAnalyze = Get-ChangedFilesFromGit ...
if ($filesToAnalyze.Count -eq 0) { ... }

After:

$filesToAnalyze = @(Get-ChangedFilesFromGit ...)
if (@($filesToAnalyze).Count -eq 0) { ... }

Files Affected

  • scripts/linting/Invoke-PSScriptAnalyzer.ps1 - 3 locations need fixing
  • scripts/security/Test-DependencyPinning.ps1 - 5 locations need fixing
  • scripts/security/Test-SHAStaleness.ps1 - 11 locations need fixing
  • scripts/linting/Invoke-YamlLint.ps1 - 3 locations need fixing
  • scripts/linting/Validate-MarkdownFrontmatter.ps1 - 3 locations need fixing
  • scripts/linting/Markdown-Link-Check.ps1 - 1 location needs fixing

Total: 6 scripts, 26 locations requiring fixes

Steps to Reproduce

  1. Run any of the affected scripts when helper functions return null or empty collections:

    • scripts/linting/Invoke-PSScriptAnalyzer.ps1
    • scripts/security/Test-DependencyPinning.ps1
    • scripts/security/Test-SHAStaleness.ps1
    • scripts/linting/Invoke-YamlLint.ps1
    • scripts/linting/Validate-MarkdownFrontmatter.ps1
    • scripts/linting/Markdown-Link-Check.ps1
  2. Observe the error: "The property 'Count' cannot be found on this object"

Additional Context

This issue was discovered in PR #639 workflow runs where the PowerShell Lint, YAML Lint, and Validate SHA Pinning Compliance checks are failing. Implementing the proposed fix will ensure robust handling of collection results throughout all PowerShell automation scripts.

The @() array subexpression operator in PowerShell:

  • Converts null to an empty array
  • Ensures single items become single-element arrays
  • Leaves arrays unchanged
  • Always provides a .Count property

This defensive programming pattern should be applied consistently across all PowerShell scripts in the repository when working with collections from function returns.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions