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

Status : 

 


1
0
Sign in
to vote
ID 773305 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 12/3/2012 10:42:35 AM
Access Restriction Public

Description

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
Sign in to post a comment.