You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set-StrictMode 2: False positive for references to the Count property #16427
PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists.
Error details
With Set-StrictMode -Off the (Get-ChildItem $TempFolder).Count return 1 which shows that the property does exist
Environment data
Name Value
---------
PSVersion 7.2.0
PSEdition Core
GitCommitId 7.2.0
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
iRon7 This is by design. Strict mode emits errors when you reference a property that does not exist. As a workaround you can coerce the result into an array type:
As a workaround you can coerce the result into an array type:
What is the catch?
What is the difference behind the scene?
Apparently there is no direct relation with member enumeration as this doesn't give an error:
([PSCustomObject]@{ Count=3 }).Count
So I guess it has something to do with an deferred execution.
I used to state that assigning a pipeline or using parenthesis will break it, now I have second thoughts whether this is always the case for using parenthesis. I suspect now that:
There is no catch. The above syntax simply wraps the object in an array so that the Count property is valid, when in strict mode. PowerShell has rules about when to unwrap an array type.
Count is also a binder construct though, similar to the ForEach and Where methods. It's supposed to exist on any object including scalars. I'm not necessarily trying to assert that it shouldn't error, I just want to make sure that's taken into account.
Count not only exists on every object, it is also explicitly (and magically) present on no object at all. $null.Count gives 0
The behavior of .Count, (0 for null, 1 on any scalar object, the number of elements in collections) is intended, extremely useful, and documented.
StrictMode is not aware of the very special semantics of .Count, so you basically can't use the two of them at the same time.
Thanks for the answers and information.
It took me a while to see this thru but Get-ChildItem $TempFolder could potentially return nothing (System.Management.Automation.Internal.AutomationNull) and () indeed doesn't have a Count property
But you can use Count on 'nothing' and it will return zero.
And for a single object you get one.
No 'real' property is needed .
That is the beauty of the feature.
This wasn't there for the first five years but was added in version 3.
Experience showed that you often had to use the work around suggested above by Paul Higinbotham (@PaulHigin) where you create an array for single objects. (A little ugly and with non-zero perf cost)
In that version we also got the equally magic feature where where you can treat any object like an array, so this works and gives 1:
$a = 1
$a[0]
Interestingly, StrictMode seems to be aware of this 'synthetic indexing operator' and does not complain, but barfs on the the closely related 'synthetic Count property'
This issue is really a valid bug report and deserves to stay open.
I don't have permissions to reopen (Rain Sallow (/u/ta11ow) (@vexx32)?) but the Count engine intrinsic property works fine on autonull in non-strict (returns 0) so I'm inclined to say it should do the same in strict. If it returned nothing I would say the error makes sense.
Reacted by penser-jata and Rain Sallow (/u/ta11ow)
That makes sense to me. If strict mode is intended to by design restrict usage of otherwise ubiquitous properties provided by the engine, that should be well documented. Given that it's not (as far as I'm aware?), I would be inclined to assume it's more of an accident of implementation and we should see if it's feasible to rectify the issue.
Reacted by Patrick Meinecke, iRon7, penser-jata and Michael Klement
your [string[]]$StringArray parameter example can be reduced to $null.Count, because unbound reference-type parameters (without default values) are $null.
changed the title [-]`Set-StrictMode -Version Latest`: False positive for references to non-existent properties of an object[/-][+]`Set-StrictMode 2`: False positive for references to the `Count` property[/+]on Nov 29, 2021
#15526 also discusses the inconsistency, but argues for the opposite resolution; to borrow from my comment there:
Engine-supplied members such as .Count, .Length, scalar [...], .psobject, ForEach(), .... now have an official name: intrinsic members.
It makes much more sense to make them consistently available, irrespective of strict mode.
This is already true for most of them, but inexplicably not for .Count and .Length ([pscustomobject] being the only exception), despite their tremendous usefulness in unified handling of scalars and collections.
Simplest repro:
# .Count fails on all types without their own .Count property *except* [pscustomobject]`# Applies analogously to .Length& { Set-StrictMode-Version 2; [pscustomobject] @{ foo=1 }.Count; 'foo'.Count; $null.Count }
iRon7, your [string[]] $StringArray parameter example can be reduced to $null.Count, because unbound reference-type parameters (without default values) are $null.
Activity
PaulHigin commentedon Nov 15, 2021
@WG-Security
iRon7 This is by design. Strict mode emits errors when you reference a property that does not exist. As a workaround you can coerce the result into an array type:
iRon7 commentedon Nov 16, 2021
Paul Higinbotham (@PaulHigin),
Thanks for the answer.
What is the catch?
What is the difference behind the scene?
Apparently there is no direct relation with member enumeration as this doesn't give an error:
So I guess it has something to do with an deferred execution.
I used to state that assigning a pipeline or using parenthesis will break it, now I have second thoughts whether this is always the case for using parenthesis. I suspect now that:
is smart and actually still emitting each item one by one despite the parenthesis.
And only when it is forced to an array it will choke the pipeline:
Or???
PaulHigin commentedon Nov 16, 2021
There is no catch. The above syntax simply wraps the object in an array so that the
Countproperty is valid, when in strict mode. PowerShell has rules about when to unwrap an array type.https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.2
SeeminglyScience commentedon Nov 16, 2021
Countis also a binder construct though, similar to theForEachandWheremethods. It's supposed to exist on any object including scalars. I'm not necessarily trying to assert that it shouldn't error, I just want to make sure that's taken into account.penser-jata commentedon Nov 17, 2021
Countnot only exists on every object, it is also explicitly (and magically) present on no object at all.$null.Countgives 0The behavior of
.Count, (0 for null, 1 on any scalar object, the number of elements in collections) is intended, extremely useful, and documented.StrictMode is not aware of the very special semantics of
.Count, so you basically can't use the two of them at the same time.iRon7 commentedon Nov 17, 2021
Thanks for the answers and information.
It took me a while to see this thru but
Get-ChildItem $TempFoldercould potentially return nothing (System.Management.Automation.Internal.AutomationNull) and()indeed doesn't have aCountpropertyI have closed the issue (
Resolution-Answered).penser-jata commentedon Nov 17, 2021
But you can use Count on 'nothing' and it will return zero.
And for a single object you get one.
No 'real' property is needed .
That is the beauty of the feature.
This wasn't there for the first five years but was added in version 3.
Experience showed that you often had to use the work around suggested above by Paul Higinbotham (@PaulHigin) where you create an array for single objects. (A little ugly and with non-zero perf cost)
In that version we also got the equally magic feature where where you can treat any object like an array, so this works and gives 1:
Interestingly, StrictMode seems to be aware of this 'synthetic indexing operator' and does not complain, but barfs on the the closely related 'synthetic Count property'
This issue is really a valid bug report and deserves to stay open.
iRon7 commentedon Nov 17, 2021
Ok, I am not able to qualify this further. To prevent that I have reopen/close it repeatedly, I will leave it up to you (Patrick Meinecke (@SeeminglyScience), Ilya (@iSazonov) ?) to change it again.
SeeminglyScience commentedon Nov 17, 2021
I don't have permissions to reopen (Rain Sallow (/u/ta11ow) (@vexx32)?) but the
Countengine intrinsic property works fine on autonull in non-strict (returns0) so I'm inclined to say it should do the same in strict. If it returned nothing I would say the error makes sense.vexx32 commentedon Nov 17, 2021
That makes sense to me. If strict mode is intended to by design restrict usage of otherwise ubiquitous properties provided by the engine, that should be well documented. Given that it's not (as far as I'm aware?), I would be inclined to assume it's more of an accident of implementation and we should see if it's feasible to rectify the issue.
iRon7 commentedon Nov 29, 2021
Something similar happens with the
Countproperty of default array parameters:The workaround is to default it to an empty array:
[String[]]$StringArray = @()👎 This isn't the same issue as initial reported, see @mklement0's comment [on 7 Dec 2021]:
[-]`Set-StrictMode -Version Latest`: False positive for references to non-existent properties of an object[/-][+]`Set-StrictMode 2`: False positive for references to the `Count` property[/+]PaulHigin commentedon Nov 29, 2021
@WG-Security
We do not see a security issue here, as strict mode is not a security feature. This is a language issue and we will update the label.
mklement0 commentedon Dec 7, 2021
Note that this problematic behavior was first reported in
#15526 also discusses the inconsistency, but argues for the opposite resolution; to borrow from my comment there:
Engine-supplied members such as
.Count,.Length, scalar[...],.psobject,ForEach(), .... now have an official name: intrinsic members.It makes much more sense to make them consistently available, irrespective of strict mode.
This is already true for most of them, but inexplicably not for
.Countand.Length([pscustomobject]being the only exception), despite their tremendous usefulness in unified handling of scalars and collections.Simplest repro:
iRon7, your
[string[]] $StringArrayparameter example can be reduced to$null.Count, because unbound reference-type parameters (without default values) are$null.iRon7 commentedon Nov 15, 2023
I guess this issue should be marked as Issue-Bug.
Anyways I have closed this one, assuming that the linked issue #2798 stays open.