Skip to content

Commit d10e4b0

Browse files
committed
Fixed Error from: Get-ParameterInfo using .Count
- It explicitly calls property 'count', to prevent errors when there is a key named 'count' - Fixed Error: Cannot compare "System.Management.Automation.ParameterMetadata" because it is not IComparable. - Created a Pester5 test to confirm it's working - Converted non-interpolated quotes to single quotes
1 parent f9006ba commit d10e4b0

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

Tests/Get-ParameterInfo.tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }
2+
3+
BeforeAll {
4+
Import-Module "$PSScriptRoot/../PSScriptTools.psm1" -Force # -verbose
5+
}
6+
7+
Describe 'Get-ParameterInfo' {
8+
It "Should Not Compare <_>'s -Count Parameter" -ForEach @(
9+
Get-Command 'Get-Random'
10+
# You only need to test one case, it either works, or not.
11+
# The Hard-coded test is a lot faster, I Kept the query to document why Get-Random was chosen.
12+
# Get-Command | Where-Object { $_.Parameters.Keys -contains 'Count' } | Select-Object -First 1
13+
) {
14+
{
15+
Get-Command $_ | Get-ParameterInfo -ea stop
16+
} | Should -Not -Throw -Because 'Key "count" was shadowing the dictionary property'
17+
}
18+
}

functions/Get-Parameter.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ Function Get-ParameterInfo {
5454
Catch {
5555
Write-Warning "Failed to find command $command"
5656
}
57-
#keep going if parameters were found
58-
if ($data.count -gt 0) {
57+
58+
# keep going if parameters were found
59+
# Explicitly calling base, to prevent .count from being shadowed
60+
if ($data.psbase.Count -gt 0) {
5961
#$data is a hash table
6062
if ($Parameter) {
6163
Write-Verbose "Getting parameter $Parameter"
@@ -67,8 +69,8 @@ Function Get-ParameterInfo {
6769
}
6870
}
6971
else {
70-
Write-Verbose "Getting parameter all non-common parameters"
71-
$params = $data.keys | Where-Object {$common -notcontains $_}
72+
Write-Verbose 'Getting parameter all non-common parameters'
73+
$params = $data.keys | Where-Object { $common -notcontains $_ }
7274
}
7375
$count = ($params | Measure-Object).count
7476
#only keep going if non-common parameters were found
@@ -81,26 +83,26 @@ Function Get-ParameterInfo {
8183
$name = $_
8284
Write-Verbose "Analyzing $name"
8385
$type = $data.item($name).ParameterType
84-
$aliases = $data.item($name).Aliases -join ","
86+
$aliases = $data.item($name).Aliases -join ','
8587

8688
$sets = $data.item($name).ParameterSets.Keys
8789
$IsDynamic = $data.item($name).IsDynamic
8890
foreach ($set in $sets) {
8991

9092
#retrieve parameter attribute class
91-
$attributes = $data.item($name).Attributes | Where-Object {$_ -is [system.management.automation.parameterAttribute] -AND $_.ParameterSetName -eq $set}
93+
$attributes = $data.item($name).Attributes | Where-Object { $_ -is [system.management.automation.parameterAttribute] -AND $_.ParameterSetName -eq $set }
9294

9395
#a parameter could have different positions in different property sets
9496
if ($attributes.position -ge 0) {
9597
$position = $attributes.position
9698
}
9799
else {
98-
$position = "Named"
100+
$position = 'Named'
99101
}
100102

101103
#write a custom object to the pipeline
102104
[PSCustomObject]@{
103-
PSTypeName = "PSParameterInfo"
105+
PSTypeName = 'PSParameterInfo'
104106
Name = $name
105107
Aliases = $aliases
106108
Mandatory = $attributes.mandatory
@@ -125,5 +127,3 @@ Function Get-ParameterInfo {
125127
} #end
126128

127129
} #end function
128-
129-

0 commit comments

Comments
 (0)