DESCRIPTION
This is a random password generation script that allows for values to be specified for minimum number of numeric, lower case letters, and upper case characters. The length is also variable, and will be a random length between $MinPwdLen and $MaxPwdLen.
Note that there are considerably simpler versions of password generation scripts available, however, these do not guarantee adherence to simple complexity rules.
SCRIPT
#*********************************************************************************
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
#
# IF YOU WANT THIS FUNCTIONALITY TO BE CONDITIONALLY SUPPORTED,
# PLEASE CONTACT ONE IDENTITY PROFESSIONAL SERVICES.
#*********************************************************************************
# Description
#
# This is a random password generation script that allows for values to be
# specified for minimum number of numeric, lower case letters, and upper case
# characters. The length is also variable, and will be a random length between
# $MinPwdLen alhd $MaxPwdLen.
#
# Note that there are considerably simpler versions of password generation
# scripts available (notably http://tinyurl.com/27vaq67), however these do not
# guarantee adherence to simple complexity rules.
#
#*******************************************************************************
Set-Variable -Name NumericChar -Value 2 -Option Constant
Set-Variable -Name UpperCaseChar -Value 2 -Option Constant
Set-Variable -Name LowerCaseChar -Value 2 -Option Constant
Set-Variable -Name MinPwdLen -Value 8 -Option Constant
Set-Variable -Name MaxPwdLen -Value 12 -Option Constant
$rand = New-Object System.Random
$Password = $null
if (($NumericChar + $UpperCaseChar + $LowerCaseChar) -gt $MaxPwdLen) {
$MaxPwdLen = $NumericChar + $UpperCaseChar + $LowerCaseChar
}
$PwdLen = $rand.Next($MinPwdLen,$MaxPwdLen+1)
$Password = @(1..$PwdLen)
$PwdChrIndices = New-Object System.Collections.ArrayList
$PwdChrIndices.AddRange(1..$PwdLen)
for ($i=1; $i -le $NumericChar; $i++) {
$ChrIndex = $rand.Next(1,$PwdChrIndices.Count + 1) - 1
$IndexAtIndex = $PwdChrIndices[$ChrIndex] - 1
$PwdChrIndices.Remove($ChrIndex)
$count = $PwdChrIndices.Count
$PwdChar = [char]($rand.Next(1,10)-1+48)
$Password[$IndexAtIndex] = [string]$PwdChar
}
for ($i=1; $i -le $UpperCaseChar; $i++) {
$ChrIndex = $rand.Next(1,$PwdChrIndices.Count+1) - 1
$IndexAtIndex = $PwdChrIndices[$ChrIndex] - 1
$PwdChrIndices.Remove($ChrIndex)
$PwdChar = [char]($rand.Next(1,26)+65)
$Password[$IndexAtIndex] = [string]$PwdChar
}
for ($i=1; $i -le $LowerCaseChar; $i++) {
$ChrIndex = $rand.Next(1,$PwdChrIndices.Count+1) - 1
$IndexAtIndex = $PwdChrIndices[$ChrIndex] - 1
$PwdChrIndices.Remove($ChrIndex)
$PwdChar = [char]($rand.Next(1,26)+97)
$Password[$IndexAtIndex] = [string]$PwdChar
}
while ($PwdChrIndices.Count -gt 0) {
$ChrIndex = $rand.Next(1,$PwdChrIndices.Count+1) - 1
$IndexAtIndex = $PwdChrIndices[$ChrIndex] - 1
$PwdChrIndices.RemoveAt($ChrIndex)
$PwdChar = $rand.Next(1,63)
if (1..10 -contains $PwdChar) {
$PwdChar = [char]($PwdChar+47)
}
elseif (11..36 -contains $PwdChar) {
$PwdChar = [char]($PwdChar-11+65)
}
elseif (37..62 -contains $PwdChar) {
$PwdChar = [char]($PwdChar-37+97)
}
$Password[$IndexAtIndex] = [string]$PwdChar
}
[string]::Join("", $Password)
#***** END OF CODE ***************************************************************