Skip to content

[Powershell] Enabling a testaccount with a randomly generated password

Another script to share, it’s quick and dirty but it does what it’s supposed to do so I might as well share it with whoever needs it.

The script was written for a hosted environment with three customers, each with a seperate test account. The test accounts are disabled at the end of the day by a scheduled task and can be enabled when needed by running this script.

The person running the script fills in the customer name and based on that it enables the right account and gives it a randomly generated password.

<#

## Description ##
   This script is used to enable the test account with a randomly generated password for a specific customer.
      
   By:  Stefan van Bruggen
   For: De Koning
	    
 
## Current Version: 1.0 ##

   Changelog:
   ----------------
   v1.0: First release
 
#>

# Load Prerequisites
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Add-Type -AssemblyName System.Web

# Check for Active Directory module and import it
if (-not (Get-Module ActiveDirectory)){            
  Import-Module ActiveDirectory            
}    

# Create function for generating a random password
Function New-RandomPassword {
$Password = "!?@#$%^&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".tochararray()
($Password | Get-Random -Count 10) -Join ''
}

# Set global variables
$Cust1 = "testcust1"
$Cust2 = "testcust2"
$Cust3 = "testcust3"

# Set variables
$input = [Microsoft.VisualBasic.Interaction]::InputBox('Enter customer name', 'Enable Test Account', 'Customer1/Customer2/Customer3') 
$random = New-RandomPassword
$password = ConvertTo-SecureString -String "$random" -AsPlainText –Force
$customer = $input.ToString()

# Enable the test-account and set a randomly generated password
if (!$customer) { write-host "Please enter customer name" -ForegroundColor Red}

if ($customer -eq "Customer1") {write-host "Enabling $Cust1" -ForegroundColor Yellow
                        Enable-ADAccount $Cust1
                        Set-ADAccountPassword -Identity $Cust1 -NewPassword $password}

if ($customer -eq "Customer2") {write-host "Enabling $Cust2" -ForegroundColor Yellow
                        Enable-ADAccount $Cust2
                        Set-ADAccountPassword -Identity $Cust2 -NewPassword $password}

if ($customer -eq "Customer3") {write-host "Enabling $Cust3" -ForegroundColor Yellow
                        Enable-ADAccount $Cust3
                        Set-ADAccountPassword -Identity $Cust3 -NewPassword $password}

# Show the generated password
Write-Host "Account password has been reset to:" -ForegroundColor Green
Write-Host ""
Write-Host "$random" -ForegroundColor Yellow
Write-Host ""
Write-Host ""

# Exit after input
Read-Host " Press enter to exit "
exit

 

Published inPowershell
Stefan van Bruggen - 2019