I am working on a script in which a number of arrays exist, these arrays contain a set of characters. But one or more of these arrays can also be empty, depending of the circumstances. At the end I need to join the content of these arrays together and then get a random character from this array.
As long as all arrays contain at least 1 character, all is fine, but if there is an empty array the Get-Random cmdlet throws the error:
Get-Random : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
So just to vizualize:
$array1 = "a","b","c" $array2 = "d","e","f" $array3 = "g","h" $joinedarray = $array1 + $array2 + $array3 $joinedarray | Get-Random -Count 1
So in above example all goes fine. But if one of the arrays is empty, Get-Random throws the above quoted error. I suppose I could check if the array is empty before adding it to the $joinedarray, but not sure how to exactly translate that in my code.