Apple iTunes
 

Here we have a simple function to generate a complex password in Windows Powershell.

To Add this to YOUR script, include:

  1. The system.web assembly (add this somewhere in your script
    (where it will only get called once)
  2. The GenPwd function

That’s all there is to it. Passwords will be 8 characters long;  change the $pwdLen variable if you want a different length.

The system.web.generatepassword call will guarantee that at least one “special” non-alphanumeric character is used but does not guarantee anthing more.

To ensure other complexities are present, we test the new password against a regular expression that tests for:

  • at least one uppercase character
  • at least one lowercase character
  • at least one numeric character

If the generated password doesn’t match the regular expression,  we keep retrying until it does.

In this sample script, the function  “GenPwd” is called  in two ways:

  1. TestMode shows how many tries it takes to generate a password
  2. Real Mode simply returns a password from the function call

 

# Sample Program to demonstrate useage of the GenPwd function call
#
# September 12, 2011
# Courtesy of xb90@PoshTips.com
#
#       Visit http://poshtips.com for more FREE PowerShell scripts!
#
# To Add this to YOUR script, include:
# (1) The system.web assembly (add this somewhere in your script
#     where it will only get called once
# (2) The GenPwd function
#
# That's all there is to it. Passwords will be 8 characters long;
# change the $pwdLen variable if you want a different length.
#
# The system.web.generatepassword call will guarantee that at least
# one "special" non-alphanumeric character is used but does not guarantee
# anthing more.
# To ensure other complexities are present, we test the new password
# against a regular expression that tests for:
#      (1) at least one uppercase character
#      (2) at least one lowercase character
#      (3) at least one numeric character
# If the generated password doesn't match the regular expression,
# we keep retrying until it does.
#
# Sort of crude, but effective and regenerating passwords is very quick
#
 
[Reflection.Assembly]::LoadWithPartialName(“System.Web”) | out-null
 
function GenPwd {
    param ([int]$pwdLen=8,
           [switch]$TestMode=$false,
           [string]$TestModeNote="")
    $pwd = ""
    $pwdOk = $false
    $tries = 0
    do {
        $tries+=1
        $pwd = [System.Web.Security.Membership]::GeneratePassword($pwdLen,1)
        $pwdOk = ($pwd -match "^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{$pwdLen,$pwdLen}$")
        } until ($pwdOk)
    if ($testMode){
        # TestMode will display the following
        #   1. Content of $TestModeNote (this could be a counter, hostname, or whatever (including nothing)
        #   2. The generated password
        #   3. Number of tries it took for the GeneratePassword method to create a qualifying password
        #   4. A series of "X"'s to give a quick graphic representation of the number of tries
        write-host ("{0} : {1} : {2} {3}" -f $TestModeNote,$pwd,$tries,$("".padright($tries,"X")))
        }
    else {
        # return the password
        $pwd
        }
    }
 
#PASS 1: Run 25 iterations in TestMode
for ($i=1; $i -le 25; $i++){
    GenPwd -PwdLen 8 -TestMode $true -TestModeNote ([string]$i).padleft(2," ")
	}
 
#Pass 2: Run 25 iteratins in "real" mode, and let function return values print to standard output
for ($i=1; $i -le 25; $i++){
    GenPwd
    #Note, I could also capture output as follows
    #    $password = GenPwd
    }

Sample output:

PS C:\scripts\powershell> .\Generate-Password.ps1
 1 : 0;5e!2Vy : 2 XX
 2 : fG8vki:5 : 3 XXX
 3 : it!9y(fO : 1 X
 4 : Q;3oCO8X : 1 X
 5 : QIt4o/$w : 1 X
 6 : *NOtk?h8 : 1 X
 7 : x]+j$5z_ : 2 XX
 8 : V/Il2%y@ : 1 X
 9 : /f=2PDlx : 2 XX
 10 : 3:C:.vYJ : 2 XX
 11 : r:Od0Uq$ : 1 X
 12 : QF42G)RY : 2 XX
 13 : .=Q>mlb2 : 3 XXX
 14 : #0URG+g{ : 1 X
 15 : n+l1k:Da : 2 XX
 16 : jh=y{8a^ : 4 XXXX
 17 : |XE41n*l : 1 X
 18 : 51Do!lAg : 2 XX
 19 : u9!y>X>n : 1 X
 20 : K@r55V#3 : 2 XX
 21 : Fy5caY=K : 1 X
 22 : i9/wdb&o : 2 XX
 23 : 3$Y(_M-? : 1 X
 24 : h:Bxbn0! : 1 X
 25 : rX(A9!gn : 1 X
 &=|2iqFp
 b=5!cs5/
 Q;#0q7#?
 -i_U1#gI
 })$Ucn74
 H2Zar>wN
 //$h3Re8
 -4nl/i5%
 cpWaH=O7
 5d&I+3V0
 7%Ub^31H
 9w>2kB7Y
 Z.6R&kVL
 89B($2h*
 0U7_oeZr
 750)#^ke
 !b?w;2T#
 ZUF6R>h7
 ]>l>q5=%
 I08gB&]S
 5c)>O_^a
 3Bw>9OTE
 (}y{lL|4
 $=}f65p4
 :rB879ql

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Posh Tips Suffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin