Search

get-random doesn't validate $null variables without '-inputobjec' by rferrisx

Active

1
0
Sign in
to vote
Type: Bug
ID: 773305
Opened: 12/3/2012 10:42:35 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
Get-random ignores a null or undefined variable in Powershell 3.0

$a=$null

(get-random -debug $a) | fl *
1333117736

(get-random -debug -verbose $a) | fl *
1221737207


(get-random -debug -verbose $null) | fl *
1050644159

$a="random string"
(get-random -debug -verbose $a) | fl *
get-random : Cannot convert value "random string" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:2
+ (get-random -debug -verbose $a) | fl *
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : InvalidArgument: (:) [Get-Random], PSInvalidCastException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger,Microsoft.PowerShell.Commands.GetRandomCommand

(get-random -debug -verbose $true) | fl *
0.872889721241263

(get-random -debug -verbose $false) | fl *
get-random : Minimum (0) cannot be greater than or equal to Maximum (0).
At line:1 char:2
+ (get-random -debug -verbose $false) | fl *
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : InvalidArgument: (:) [Get-Random], ArgumentException
    + FullyQualifiedErrorId : MinGreaterThanOrEqualMax,Microsoft.PowerShell.Commands.GetRandomCommand


See my blog post here for how this effects the '-max' and '-min' parameters as well:
http://horizontal-logic.blogspot.com/2012/12/a-defect-for-get-random.html
Details (expand)
How often does this happen?
Always Happens

Have you seen this problem before in this product?

I don't know if this issue existed previously
Reproduction Steps
(1) Invoke 'get-random' with any previously undefined variable or a variable defined as $null
(2) A default random number is returned.

See my blog post here for how this effects the '-max' and '-min' parameters as well:
http://horizontal-logic.blogspot.com/2012/12/a-defect-for-get-random.html
Expected Results
Get-random should reject single $null arguments with an error action instead of proceeding without error.

The problem here is that get-random generates a value in response to a $null variable. If your script mistakenly does not define a critical limit, then you many not notice get-random invoking a silent error action by default. At least you have to document this behavior so a critical script could use something like:

If (!defined_variable) {} else {get-random defined_variable}

It is true that using '-inputobject' avoids this result. So noting that in the documentation would be useful. For example:

get-random -inputobject $a
Get-Random : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the comma
again.
At line:1 char:25
+ get-random -inputobject $a
+                         ~~
    + CategoryInfo         : InvalidData: (:) [Get-Random], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetRandomCommand
File Attachments
0 attachments
Sign in to post a comment.
Sign in to post a workaround.