Friday, March 27, 2015

#49 : Generating Random Numbers with Powershell


I never got a requirement where I need to generate a random number in Powershell. The reason is that I never develop something like game or script which runs on chance.  But there is one script which might need random number. The script will be all about generating complex passwords randomly. The script will generate a number between 1 and 26 to get a number and then a letter is generate from that number. The loop goes for as many characters you want in your random password. We will talk about the program in next post. For now, let's play with get-random cmdlet.

The cmdlet to be used is :Get-Random. Please review the syntax below. If syntax looks horrible, kindly take look in to examples, I bet you will not close this tab in your browser.

Syntax:

Get-Random [-InputObject] Object[] [-Count int] [-SetSeed int] [CommonParameters]



Example:

[1] Generate a number between 0 and 100.

Get-random -Minimum 0 -Maximum 100

- Run it multiple time and you will see a new number everytime.

[2] Generate a number within the list provided

get-random -input 8, 4, 12, 20, 24, 108 -count 1

Conclusion:

Get-Random cmdlet can be used for several purpose. At least, I know one purpose which is all about generating a random complex password. I will write the script and post very soon. The viewership of this blog has increased alot and people like and send their mails to me. I think, the time has come when I should write some well qualified scripts and publish in this blog, hope you all will like them.

Thanks!


Get-Random [-InputObject] Object[] [-Count int] [-SetSeed int] [CommonParameters]






No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...