Password Generator
The original script was given to us as a lab assignment in WIN213 (Windows PowerShell Scriptng)
My own version of the script can be downloaded from the link below:
BJCairney_passwdgen.ps1
The Problem:
The original Lab instructions came with a txt file containing user names and we were to create an auto-generated password for each username. The students were to then manually copy and paste the names from the text file along with the first round of auto generated passwords into a here string. The script was to be run once more, with the added step of converting the here string into a hash table, displaying the hash table on the screen, and sending everything to a txt file.
As is evident in the image below, the names and the passwords on the top half of the screen did not match when they were converted to a hash table on the bottom half. The passwords were also different because of the script being run more than once.
I wanted to find a way to have the script generate a list of passwords for each name only once, and convert that information into a hash table. I also wanted to find a way to change the display of the hash table headings to match the values that they were pertaining to.
My own version of the script can be downloaded from the link below:
BJCairney_passwdgen.ps1
The Problem:
The original Lab instructions came with a txt file containing user names and we were to create an auto-generated password for each username. The students were to then manually copy and paste the names from the text file along with the first round of auto generated passwords into a here string. The script was to be run once more, with the added step of converting the here string into a hash table, displaying the hash table on the screen, and sending everything to a txt file.
As is evident in the image below, the names and the passwords on the top half of the screen did not match when they were converted to a hash table on the bottom half. The passwords were also different because of the script being run more than once.
I wanted to find a way to have the script generate a list of passwords for each name only once, and convert that information into a hash table. I also wanted to find a way to change the display of the hash table headings to match the values that they were pertaining to.
The output of the original script is as follows:
The Solution:
I consolidated the steps of creating a here string and generating a password into one step. I created an array to contain the user names and I ran a for loop through the array. I appended the name and password for each user to the here string through each iteration of the loop. I then converted the here string into a hash table with custom labels.
After adding my own code to the assignment, the output is as follows:
I wrote the following script to accomplish this:
<#
################################################################################
################################################################################
## FileName: Lab8_BJCairney_pswdgen.ps1
## Author: Brian Cairney Date: July 4, 2014 DateLastModified: July 4, 2014
## Purpose: Creates a randomly generated password for 10 users and stores them
## in a txt file.
## Synatx: No special instructions
##############################################################################
##############################################################################
#>
Clear-Host
## Preparation
## In case script has been run once in ISE, check for $Herestring and clear if present
#######################################################################################
$TestForHereString = Test-Path Variable:\HereString
If($TestForHereString -eq $True){
Clear-Variable HereString
}
## Exercise 1
## - Creating Arrays of Character Sets
###############################################################################
$LowerCase1 = @("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
$UpperCase1 = @("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
$Digit = @(1..9)
$Symbol = @("!","@","#","$","%","^","&","*","?","+")
$LowerCase2 = @("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
$UpperCase2 = @("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
## Exercise 2
## - Generating a Random Password
## AND
## Exercise 3: Challenge –Creating a Hash Table of Username=Password
#################################################################################
## Part 1
## - Create an array of usernames.
#################################################################################
$Usernames = @("Mike Baker","Mary Martin","Uma Siveram","Uri Demisky","Steven Sullivan","Tom Mills","Gary Parker", `
"Ahmed Mohammad","Rhonda Jinkins","Gord Routley")
## Part 2
## - Create a password for each name using a ForEach loop
#################################################################################
Foreach($Name in $Usernames){
$Password = ($Lowercase1 |Get-Random)
$Password += ($Uppercase1 |Get-Random)
$Password += ($Digit |Get-Random)
$Password += ($Symbol |Get-Random)
$Password += ($LowerCase2 |Get-Random)
$Password += ($UpperCase2 |Get-Random)
$Password += "`n"
## Part 3
## - Create a here string for converting to a hashtable and append each user/pword
##################################################################################
$HereString += @"
$Name = $Password
"@
}
## Part 4
## - Convert the here string to a hash table
###################################################################################
$UserNamePasswordHashTable = ConvertFrom-StringData -StringData $HereString
## Part 5
## - Create a custom output format for format-table and pipe The HashTable into
## Format-Table, and Output to the file L8_userpasswords.txt
###################################################################################
#Test if file exsists, and if not create it
$PasswordFilePath = "$Home\L8_userpasswords.txt"
$TestPasswordFile = Test-Path $PasswordFilePath
If($TestPasswordFile -eq $False){
New-Item -Path $PasswordFilePath -ItemType File |Out-Null
}
$TableLayout = @{Expression={$_.Name};Label="User Name";width = 30},@{Expression={$_.Value};Label="Password"}
$UserNamePasswordHashTable |Format-Table $TableLayout |Out-File $PasswordfilePath
## Part 6
## - Send the hash table to the password textfile (Appended)
###################################################################################
$UserNamePasswordHashTable |Out-File $PasswordFilePath -append
## Part 7
## - Display The file contents to the screen
####################################################################################
Get-Content $PasswordFilePath
################################################################################
################################################################################
## FileName: Lab8_BJCairney_pswdgen.ps1
## Author: Brian Cairney Date: July 4, 2014 DateLastModified: July 4, 2014
## Purpose: Creates a randomly generated password for 10 users and stores them
## in a txt file.
## Synatx: No special instructions
##############################################################################
##############################################################################
#>
Clear-Host
## Preparation
## In case script has been run once in ISE, check for $Herestring and clear if present
#######################################################################################
$TestForHereString = Test-Path Variable:\HereString
If($TestForHereString -eq $True){
Clear-Variable HereString
}
## Exercise 1
## - Creating Arrays of Character Sets
###############################################################################
$LowerCase1 = @("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
$UpperCase1 = @("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
$Digit = @(1..9)
$Symbol = @("!","@","#","$","%","^","&","*","?","+")
$LowerCase2 = @("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
$UpperCase2 = @("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
## Exercise 2
## - Generating a Random Password
## AND
## Exercise 3: Challenge –Creating a Hash Table of Username=Password
#################################################################################
## Part 1
## - Create an array of usernames.
#################################################################################
$Usernames = @("Mike Baker","Mary Martin","Uma Siveram","Uri Demisky","Steven Sullivan","Tom Mills","Gary Parker", `
"Ahmed Mohammad","Rhonda Jinkins","Gord Routley")
## Part 2
## - Create a password for each name using a ForEach loop
#################################################################################
Foreach($Name in $Usernames){
$Password = ($Lowercase1 |Get-Random)
$Password += ($Uppercase1 |Get-Random)
$Password += ($Digit |Get-Random)
$Password += ($Symbol |Get-Random)
$Password += ($LowerCase2 |Get-Random)
$Password += ($UpperCase2 |Get-Random)
$Password += "`n"
## Part 3
## - Create a here string for converting to a hashtable and append each user/pword
##################################################################################
$HereString += @"
$Name = $Password
"@
}
## Part 4
## - Convert the here string to a hash table
###################################################################################
$UserNamePasswordHashTable = ConvertFrom-StringData -StringData $HereString
## Part 5
## - Create a custom output format for format-table and pipe The HashTable into
## Format-Table, and Output to the file L8_userpasswords.txt
###################################################################################
#Test if file exsists, and if not create it
$PasswordFilePath = "$Home\L8_userpasswords.txt"
$TestPasswordFile = Test-Path $PasswordFilePath
If($TestPasswordFile -eq $False){
New-Item -Path $PasswordFilePath -ItemType File |Out-Null
}
$TableLayout = @{Expression={$_.Name};Label="User Name";width = 30},@{Expression={$_.Value};Label="Password"}
$UserNamePasswordHashTable |Format-Table $TableLayout |Out-File $PasswordfilePath
## Part 6
## - Send the hash table to the password textfile (Appended)
###################################################################################
$UserNamePasswordHashTable |Out-File $PasswordFilePath -append
## Part 7
## - Display The file contents to the screen
####################################################################################
Get-Content $PasswordFilePath
No comments:
Post a Comment