Create Random Password and update AD account

Tags:
Not to long ago I was asked to write a script that would create a random password and change an AD account to use that new password. The requirements were that the password be 3 uppercase letters 2 numbers and 3 lowercase letters. Here is what I did.
#Clear out all variables from last run
$newpassword = $NULL
$loops = 1..3
## Set up Random Generator
$rand = New-Object System.Random
$i = 1
$n = 1
#set up loop to generate each section of $NewPassword
foreach($number in $loops)
{
if ($i -eq 1)
{
$part = $NULL
$m = 3
$s = 65
$e = 90
}
if ($i -eq 2)
{
$part = $NULL
$m = 2
$s = 48
$e = 57
}
if ($i -eq 3)
{
$part = $NULL
$m = 3
$s = 97
$e = 122
}
$n..$m | foreach { $part = $part + [char]$rand.next($s,$e) }
$newpassword = $newpassword + $part
$i = $i + 1
Set-QADUser PNI-GUEST -UserPassword $newpassword
 Comment on this Post