I find it a little ironic that although I like efficiency, structure and order I'm fascinated with randomness. In today's Friday Fun I want to explore a few ways you might incorporate randomness into your PowerShell scripting. Perhaps you need a random number between 100 and 500. Or a string for file name. There are many paths so let's (randomly) take this one.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Number Namer
To get a random number is pretty easy using the Get-Random cmdlet.
[cc lang="PowerShell"]
PS C:\> get-random
729751216
[/cc]
By default the cmdlet returns a number between 0 and 2,147,483,647. But you can set the minimum and maximum values to limit the range.
[cc lang="PowerShell"]
PS C:\> get-random -min 100 -max 500
384
[/cc]
I also like that you can tell the cmdlet to return X number of random numbers, but for that you need to pipe in the collection of numbers.
[cc lang="PowerShell"]
PS C:\> 100..500 | Get-Random -count 5
365
442
171
454
111
[/cc]
Strings and Filenames
I expect that many of you have seen this technique to generate a random and temporary filename.
[cc lang="Powershell"]
PS C:\> [system.io.path]::getTempfilename()
C:\Users\Jeff\AppData\Local\Temp\tmp8F7F.tmp
[/cc]
This works great if you want the temp file in your temp folder. But what if you want to create the random or temp file in a different directory. One way might be to use the GetRandomFileName() method.
[cc lang="PowerShell"]
PS C:\> $f=[system.io.path]::GetRandomFileName()
PS C:\> $p=Join-Path "c:\work" $f
PS C:\> $p
c:\work\d1gsg31u.qoc
[/cc]
I used Join-Path to create a proper path with the folder and random file name. I always recommend Join-Path to construct file paths instead of concatenation.
But what if you want a random character string without the extension or of a different length? You could use the GetRandomFileName() method and parse the string. I decided to try something else and put together a short function to generate a random string of a user specified length.
[cc lang="PowerShell"]
Function New-RandomString {
Param([string]$source="123abcDEFg45hijk67MNO890",[int]$Length=7)
$ofs=""
[string]$NewString=$source.ToCharArray() | Get-Random -count $length
write-Output $NewString
}
[/cc]
The function takes a source string as a parameter, or you can define a default as I've done in the function. The other parameter is the length of the string. The main part of the function turns the string into a character array and then pipes it to Get-Random to retrieve the specified number of characters. The result is saved to the variable, $NewString. Notice that I have specifically cast this as a string. If I didn't, then $NewString would be an array of random characters. But there's one other trick here. Whenever you convert an array to a string, the output field separator is used between each element. This is stored in the $ofs automatic variable. The default is a space. In my function, I set it to basically nothing, so that the characters form a single string.
[cc lang="PowerShell"]
PS C:\> new-randomstring -Length 10
O5D18E72cj
[/cc]
Let's wrap this discussion up with total chaos and get a random number of strings with varying (and random length).
[cc lang="PowerShell"]
PS C:\> 1..(Get-Random -min 5 -max 10) | foreach {New-RandomString -length (Get-Random -min 1 -max 10)}
N7
Ek7i31Db
b60
Oc6F
aO
8F4
78
9N4O5
3i19OFkj0
[/cc]
I crammed everything into a one line expression. The first part defines a range of numbers from 1 to a random number between 5 and 10. These numbers act as counters for the ForEach construct which calls the New-RandomString function specifying a string of a random length between 1 and 10.
If you want to play with the New-RandomString function, you can download it here..