Powershell

Here’s a task every Network Administrator or Technician has to deal with.  Generating passwords.

There’s a few websites I traditionally use for this task.  But wait?  Isn’t it just “Random Letters”?

At least the temporary one is.  What the users choose after that is their choice but I like having something random pulled out for the users.  Would ya know it?  Powershell has a “GET-RANDOM” cmdlet.

Now there are various ways I could have done this but I started simple.   Pick one of the 26 letters in the alphabet and pull them together as a password.  Getting ASCII characters in Powershell is very easy.  Just Plug in the ASCII Number into [char]

[CHAR]65

Will produce an uppercase “A”

Not too difficult then to do something like

[CHAR]((GET-RANDOM 26)+65)

To pick a random letter from A to Z in uppercase!

So a simple loop like this

$Newpassword=$NULL; FOREACH ($Counter in 1..8) { $Newpassword=$Newpassword+([CHAR]((GET-RANDOM 26)+65)) }

Will generate a Random password of just Uppercase letters.

So then I though, “I can take this up a notch”.  I decided to make an array containing all the numeric starting points in ASCII and the amount of Characters in each set.

[int32[]]$ArrayofAscii=26,97,26,65,10,48,15,33

This array is organized by the number of characters in the sequence followed by the Starting point in Ascii.  IE: 26 Letters, Lowercase starts at 97, 26 Letters, Uppercase starts at 65

Pretty simple idea.   Now my first idea was to just go completely random and pick from the four sets.  It worked but You always got passwords like this

3jH(B(zA

Which is fine, but I want some control.  For me?  I’d love to use that daily.  But I need this to be useful for everybody.    So I decided to control how many sets you would pick from (1-4) deciding that lowercase letters were the simplest, followed by a mix of Upper/Lower, then adding in Numbers, then Punctuation for passwords.

The end result is a function called GET-NEWPassword that will generate an 8 Character password and reasonably complexity for your use.   If you want to play with the Function, one of the things you COULD pass into it would be the length and complexity.  For simplicity, I presumed most Administrators have a standard they work with and you can edit that in the function yourself to make it simpler.

Cool part is you can take this password and make it a Secure string using the following sequence

$NewUsePassword=ConvertTo-SecureString (GET-NewPassword) -AsPlainText –force

Pretty cool eh?  But there you have it.  Something you can take home and use daily.

Remember, the Power of Shell is in YOU

Sean
The Energized Tech

############### Script Begin ##################

function global:GET-NewPassword() {
# Delare an array holding what I need.  Here is the format
# The first number is a the number of characters (Ie 26 for the alphabet)
# The Second Number is WHERE it resides in the Ascii Character set
# So 26,97 will pick a random number representing a letter in Asciii
# and add it to 97 to produce the ASCII Character
#
[int32[]]$ArrayofAscii=26,97,26,65,10,48,15,33
# Complexity can be from 1 - 4 with the results being
# 1 - Pure lowercase Ascii
# 2 - Mix Uppercase and Lowercase Ascii
# 3 - Ascii Upper/Lower with Numbers
# 4 - Ascii Upper/Lower with Numbers and Punctuation
$Complexity=3
# Password Length can be from 1 to as Crazy as you want
#
$PasswordLength=8
# Nullify the Variable holding the password
$NewPassword=$NULL
# Here is our loop
Foreach ($counter in 1..$PasswordLength) {
# What we do here is pick a random pair (4 possible)
# in the array to generate out random letters / numbers
$pickSet=(GET-Random $complexity)*2
# Pick an Ascii Character and add it to the Password
# Here is the original line I was testing with
# [char] (GET-RANDOM 26) +97 Which generates
# Random Lowercase ASCII Characters
# [char] (GET-RANDOM 26) +65 Which generates
# Random Uppercase ASCII Characters
# [char] (GET-RANDOM 10) +48 Which generates
# Random Numeric ASCII Characters
# [char] (GET-RANDOM 15) +33 Which generates
# Random Punctuation ASCII Characters
$NewPassword=$NewPassword+[char]((get-random $ArrayOfAscii[$pickset])+$ArrayOfAscii[$pickset+1])
}
# When we're done we Return the $NewPassword
# BACK to the calling Party
Return $NewPassword
}
############### Script Ending ##################

 

Advertisements