diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs index 1a7c4b1a8d6..f621dd3c7b3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommand.cs @@ -28,6 +28,7 @@ public class GetRandomCommand : PSCmdlet private const string RandomNumberParameterSet = "RandomNumberParameterSet"; private const string RandomListItemParameterSet = "RandomListItemParameterSet"; + private static readonly object[] _nullInArray = new object[] { null }; private enum MyParameterSet { @@ -275,7 +276,7 @@ private double ConvertToDouble(object o, double defaultIfNull) /// List from which random elements are chosen. /// [Parameter(ParameterSetName = RandomListItemParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)] - [ValidateNotNullOrEmpty] + [System.Management.Automation.AllowNull] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public object[] InputObject { get; set; } @@ -491,9 +492,11 @@ protected override void ProcessRecord() { if (EffectiveParameterSet == MyParameterSet.RandomListItem) { - foreach (object item in InputObject) + // this allows for $null to be in an array passed to InputObject + foreach (object item in InputObject ?? _nullInArray) { - if (_numberOfProcessedListItems < Count) // (3) + // (3) + if (_numberOfProcessedListItems < Count) { Debug.Assert(_chosenListItems.Count == _numberOfProcessedListItems, "Initial K elements should all be included in chosenListItems"); _chosenListItems.Add(item); @@ -501,9 +504,12 @@ protected override void ProcessRecord() else { Debug.Assert(_chosenListItems.Count == Count, "After processing K initial elements, the length of chosenItems should stay equal to K"); - if (Generator.Next(_numberOfProcessedListItems + 1) < Count) // (1) + + // (1) + if (Generator.Next(_numberOfProcessedListItems + 1) < Count) { - int indexToReplace = Generator.Next(_chosenListItems.Count); // (2) + // (2) + int indexToReplace = Generator.Next(_chosenListItems.Count); _chosenListItems[indexToReplace] = item; } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 index d8ee1ee48bf..fa835478e98 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Random.Tests.ps1 @@ -182,4 +182,16 @@ Describe "Get-Random" -Tags "CI" { It "Should throw an error because the hexadecimal number is to large " { { Get-Random 0x07FFFFFFFFFFFFFFFF } | Should -Throw "Value was either too large or too small for a UInt32" } + + It "Should accept collection containing empty string for -InputObject" { + 1..10 | ForEach-Object { + Get-Random -InputObject @('a','b','') | Should -BeIn 'a','b','' + } + } + + It "Should accept `$null in collection for -InputObject" { + 1..10 | ForEach-Object { + Get-Random -InputObject @('a','b',$null) | Should -BeIn 'a','b',$null + } + } }