Jump to content

Recommended Posts

Posted
Does anyone know of a tool which can automatically change a particular user's AD password on a schedule, then email someone else with that password? I'm looking for a lazy/automated/can't-be-forgotten way of changing the guest teacher password on our network...
Posted
That script prompts for a password and waits for it to be entered, I'd like the script to choose the password itself and so be totally hands-off.
Posted

function Create-RandomPassword 
( 
  [int] $minLength = 8, 
  [int] $maxLength = 8, 
  [bool] $useSymbols = $false, 
  [bool] $asSecureString = $false
) 
{ 
  [system.Security.Cryptography.RNGCryptoServiceProvider] $random = 
     new-object System.Security.Cryptography.RNGCryptoServiceProvider 
   
  # Get an array of all characters that can be used in the password 
  [string] $choice = Get-CharacterChoice -useSymbols $useSymbols 
   
  $randomPassword = $null 
  if (($minLength -le $maxLength) -and 
     ($minLength -ge 6)) 
  { 
     # Allocate a byte array of dimension 1 
     $randomNumber = new-object byte[] 1 
     if ($minLength -eq $maxLength) 
     { 
        [int] $length = $minLength 
     } 
     else 
     { 
        # Calculate a random length between minLength and maxLength 
        $random.GetBytes($randomNumber) 
        [int] $length = $minLength + $randomNumber[0] % 
                    ($maxLength - $minLength + 1) 
     } 
 
     # Allocate a byte array of dimension $length 
     $randomSequence = new-object byte[] $length 
     $hasUCase = $hasLCase = $hasNum = $false 
     while(!$hasUCase -or !$hasLCase -or !$hasNum) 
     { 
        # Generate random sequence of bytes 
        $random.GetBytes($randomSequence) 
         
        # Ensure that there is at least one number, uppercase 
        # character and lowercase character in the sequence. 
        $hasUCase = $hasLCase = $hasNum = $false 
        foreach($b in $randomSequence) 
        { 
           [char]$char = $choice[$b % $choice.Length] 
           if ($char -ge 'A' -and $char -le 'Z') 
           { 
              $hasUCase = $true 
           } 
            
           if ($char -ge 'a' -and $char -le 'z') 
           { 
              $hasLCase = $true 
           } 
            
           if ($char -ge '0' -and $char -le '9') 
           { 
              $hasNum = $true 
           } 
        } 
     } 
 
     if ($asSecureString) 
     { 
        $randomPassword = new-object System.Security.SecureString 
     } 
     else 
     { 
        [string] $randomPassword = '' 
     } 
      
     # Assign the password from the sequence of random bytes 
     foreach($b in $randomSequence) 
     { 
        [char]$char = $choice[$b % $choice.Length] 
        if ($asSecureString) 
        { 
           $randomPassword.AppendChar($char) 
        } 
        else 
        { 
           $randomPassword += $char 
        } 
     } 
  } 
   
  return $randomPassword 
} 

# Outputs an array of all the characters that the generated password 
# can be made up of. 
function Get-CharacterChoice 
( 
  [bool] $useSymbols = $true 
) 
{ 
  if ($useSymbols) 
  { 
     [string] $choice = '!"#$%&''()*+,-./' 
  } 
  else 
  { 
     [string] $choice = '' 
  } 
   
  $choice += '23456789' 
  if ($useSymbols) 
  { 
     $choice += ':;<=>?@' 
  } 
   
  $choice += 'ABCDEFGHJKMNPQRSTUVWXYZ' 
  if ($useSymbols) 
  { 
     $choice += '[\]^_`' 
  } 
   
  $choice += 'abcdefghjkmnpqrstuvwxyz' 
  if ($useSymbols) 
  { 
     $choice += '{|}~' 
  } 
   
  return $choice 
} 

 

write-host "Importing GetPassword.ps1"
. "\\file\share\GetPassword.ps1"
$PassWord = Create-RandomPassword

 

That module (saved as GetPassword.ps1) plus code snippet should get you there.

 

The random password never contains 0o1iL.

 

Peter

Posted (edited)
That script prompts for a password and waits for it to be entered, I'd like the script to choose the password itself and so be totally hands-off.

 

Caneasily make PowerShell generate a random code where the count = how long the password is.

 

$Password = ([char[]](Get-Random -Input $(48..57 + 65..90 + 97..122) -Count 5)) -join ""
Edited by elsiegee40
  • Thanks 1
Posted

$newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 4)) -join ""

Set-ADAccountPassword -Identity "CN=Wifi Guest,OU=Guests,OU=New Students,OU=users,OU=school,DC=school,DC=local" -reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force)

[string]$body = @()

$body+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed"

$body1+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed #close"

Send-MailMessage -To [email protected]' -Body $body -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

Send-MailMessage -To '[email protected]' -Body $body1 -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

  • Thanks 2
Posted
$newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 4)) -join ""

Set-ADAccountPassword -Identity "CN=Wifi Guest,OU=Guests,OU=New Students,OU=users,OU=school,DC=school,DC=local" -reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force)

[string]$body = @()

$body+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed"

$body1+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed #close"

Send-MailMessage -To [email protected]' -Body $body -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

Send-MailMessage -To '[email protected]' -Body $body1 -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

 

Well, that's the cleanest looking one so far. We don't have an onsite SMTP server, but assuming I can configure usernames/passwords into that Send-MailMessage command, I could use our ISP-provided one.

Posted
i'd imagine it would work, if you need to auttnicate you can add smtp to iis and forward the email on. That script sends to sos which is our helpdesk, that way we have a record of the change.
  • 1 year later...
Posted
$newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 4)) -join ""

Set-ADAccountPassword -Identity "CN=Wifi Guest,OU=Guests,OU=New Students,OU=users,OU=school,DC=school,DC=local" -reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force)

[string]$body = @()

$body+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed"

$body1+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed #close"

Send-MailMessage -To [email protected]' -Body $body -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

Send-MailMessage -To '[email protected]' -Body $body1 -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

 

Thanks for this, just used it as a base for our auto reset for all my schools! :) really simple too!

  • Thanks 1
  • 2 months later...
Posted
$newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 4)) -join ""

Set-ADAccountPassword -Identity "CN=Wifi Guest,OU=Guests,OU=New Students,OU=users,OU=school,DC=school,DC=local" -reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force)

[string]$body = @()

$body+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed"

$body1+= "new password for use with wifiguest account is " + $newPassword + " Please reply to this email when password has been distributed #close"

Send-MailMessage -To [email protected]' -Body $body -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

Send-MailMessage -To '[email protected]' -Body $body1 -Subject 'New Password for wifi guest account' -from '[email protected]' -smtpServer '10.0.0.26'

 

Zombie-thread alert!

 

Is that a Powershell script which I would save as a scheduled task on the server?

 

Alternatively, has anyone come up with a different/simpler option for scheduled password changes?

Posted (edited)
Zombie-thread alert!

 

Is that a Powershell script which I would save as a scheduled task on the server?

 

Alternatively, has anyone come up with a different/simpler option for scheduled password changes?

it is a powershell script, i ended up editing it to make my life simpler moving it between schools and doing multiple accounts

 

i know i could have used loops or whatever but its my first time using powershell!

posted it here as the code thing ate the formatting

 

https://gist.github.com/dgardiner87/7cef05013a1be75e8c431a786be6b18f

Edited by DGardiner
Posted

Right, I've got this working on our system (even sending the email through Google Mail), so thanks.

 

One question - can I set any controls over the password which is generated? At present, it is generating completely random passwords like oxz3p6rw and my worry is these are more likely to get written down than if I could set passwords like river74 or whatever.

Posted
One question - can I set any controls over the password which is generated? At present, it is generating completely random passwords like oxz3p6rw and my worry is these are more likely to get written down than if I could set passwords like river74 or whatever.

I do this for our guest student password, which is then emailed to a set of teachers every morning:

 

$subjects = "Maths","Geography","History","Science","French","German","Spanish","English","Drama","Music"
$subjectnum = Get-Random -Maximum 9 -minimum 0

$number = Get-Random -Maximum 999 -Minimum 111

$randomPasswordTxt = $subjects[$subjectnum] + $number
$randomPassword = ConvertTo-SecureString -string $randomPasswordTxt -AsPlainText -force

 

Generates passwords like Maths519, English726, Music931...

  • Thanks 1
Posted (edited)
I do this for our guest student password, which is then emailed to a set of teachers every morning:

 

$subjects = "Maths","Geography","History","Science","French","German","Spanish","English","Drama","Music"
$subjectnum = Get-Random -Maximum 9 -minimum 0

$number = Get-Random -Maximum 999 -Minimum 111

$randomPasswordTxt = $subjects[$subjectnum] + $number
$randomPassword = ConvertTo-SecureString -string $randomPasswordTxt -AsPlainText -force

 

Generates passwords like Maths519, English726, Music931...

 

Right, that I like. Whereabouts do I put that code string in my PS script though? Do I just replace $newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 8)) -join "" with your code?

Edited by enjay
Posted
Right, that I like. Whereabouts do I put that code string in my PS script though? Do I just replace $newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 8)) -join "" with your code?

 

put his/her code above all of yours

 

and change the line:

$newPassword = ([char[]](Get-Random -Input $(48..57 + 97..122) -Count 4)) -join ""

 

to

 

$newPassword = $randomPassword

 

 

i think!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...