-
Notifications
You must be signed in to change notification settings - Fork 125
Description
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:
- PowerShell Lint (
Invoke-PSScriptAnalyzer.ps1) - 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 fixingscripts/security/Test-DependencyPinning.ps1- 5 locations need fixingscripts/security/Test-SHAStaleness.ps1- 11 locations need fixingscripts/linting/Invoke-YamlLint.ps1- 3 locations need fixingscripts/linting/Validate-MarkdownFrontmatter.ps1- 3 locations need fixingscripts/linting/Markdown-Link-Check.ps1- 1 location needs fixing
Total: 6 scripts, 26 locations requiring fixes
Steps to Reproduce
-
Run any of the affected scripts when helper functions return null or empty collections:
scripts/linting/Invoke-PSScriptAnalyzer.ps1scripts/security/Test-DependencyPinning.ps1scripts/security/Test-SHAStaleness.ps1scripts/linting/Invoke-YamlLint.ps1scripts/linting/Validate-MarkdownFrontmatter.ps1scripts/linting/Markdown-Link-Check.ps1
-
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
.Countproperty
This defensive programming pattern should be applied consistently across all PowerShell scripts in the repository when working with collections from function returns.