Repository navigation
Closed
Labels
Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime
Description
Steps to reproduce
Get-Random -InputObject @('zzz', 'aaa', '')
Get-Random @('zzz', 'aaa', '')Expected behavior
InputObject parameter position value is 1, so these command should work the same way and return one of the array elements.
Actual behavior
While the second command works, first command produces error:
Get-Random : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an argument th
at is not null or empty, and then try the command again.
At line:1 char:25
+ Get-Random -InputObject @('zzz', 'aaa', '')
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Random], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetRandomCommandEnvironment data
> $PSVersionTable
Name Value
---- -----
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.3
BuildVersion 3.0.0.0
WSManStackVersion 3.0
PSVersion 6.0.0-alpha
CLRVersion
PSEdition Core
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
GitCommitId v6.0.0-alpha.18
Name Value
---- -----
PSVersion 5.1.15063.138
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.138
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1Metadata
Metadata
Assignees
Labels
Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
bnygld commentedon Aug 4, 2017
I'm seeing this behaviour too.
iSazonov commentedon Sep 28, 2019
This is a nasty bug in parameter binding.
Steve Lee (@SteveL-MSFT) Dongbo Wang (@daxian-dbw) Michael Klement (@mklement0) for information.
SteveL-MSFT commentedon Sep 28, 2019
This isn't an issue in the parameter binder. The default parameter set is for
-Maximum, not-InputObject.InputObjecthas[ValidateNotNullOrEmpty]whereasMaximumdoes not. This attribute also checks if the collection contains$null(or empty string which is equivalent for strings). And this is what's causing the error message. Removing that attribute "fixes" this issue. Would need to think through any side effects. Since that parameter is mandatory, you can't pass in$nullanyways.mklement0 commentedon Sep 28, 2019
Raph Ben (@Benny-Gold): Your example is missing the
-InputObjectparameter for reproducing the symptom; as an aside: given that the list you're retrieving by definition has no line-internal spaces, the better command to use is$domainlistMaster = -split (Invoke-RestMethod -useBasicParsing -uri $domainListSource), which bypasses the problem.Steve Lee (@SteveL-MSFT):
Removing the
ValidateNotNullOrEmptystrikes me as the right thing to do, because there is no reason to prevent pulling random elements from collections only because they happen to have empty elements.Get-Random -InputObject $null), but (a) with an[object[]]-typed parameter that emptiness check is invariably also applied to the elements of the array individually; and (b), even an empty scalar isn't worth preventing, if it is explicitly passed (which is by definition the case here, given that the parameter is mandatory), so not only should[ValidateNotNullOrEmpty]be removed,[AllowNull]should be added.As for what parameter is bound with a positional argument:
Due to trickery in GetRandomCommand.cs
-Maximumis ultimately not bound if its argument is an array: the code checks for an array-valued argument and, if so, assigns toInputObjectinstead.The upshot is:
Using an array positionally accidentally bypasses the
ValidateNotNullOrEmptyvalidation attribute, because the argument is initially bound to-Maximum, which doesn't have this attribute; therefore, it is only positional use with an array that currently - accidentally - exhibits the desired behavior.The trickery enables questionable commands such as
Get-Random -Maximum (1..10) -Count 3, which is effectively the same asGet-Random -InputObject (1..10) -Count 3SteveL-MSFT commentedon Sep 28, 2019
I'm also thinking that removing
[ValidateNotNullOrEmpty]is the right thing and shouldn't have any negative side effects. I'll submit a PR.DougChandler commentedon Apr 2, 2021
I am seeing this bug in PS 5.1. Very strange behaviour.
I am passing in an array which contains an empty string as an element as that is one of the choices I want to be chosen.
mklement0 commentedon Apr 2, 2021
Doug Chandler (@DougChandler), please note that this is repo is for PowerShell (Core) only (v6+).
For Windows PowerShell, you can file an issue at its UserVoice forum, but note that Windows PowerShell is no longer actively developed and will likely only receive critical security fixes.