Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Created December 15, 2021 19:55
Show Gist options
  • Select an option

  • Save Rugby-Ball/7f4ef79528fc0fd11b0fad7ca02d4cb2 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/7f4ef79528fc0fd11b0fad7ca02d4cb2 to your computer and use it in GitHub Desktop.
A Function used to make a password, Can set length and Complexity of password. #Security #Utility #Public #Function
# get-password.ps1
<#
Description: A Function used to make a password, Can set length and Complexity of password.
Written: Ed Walsh
PowerShell.Core tested: Not Tested
Version: 1.0.0
Create Date: 12/15/2021
Revised Date: 12/15/2021
#>
function Get-Password($length, $complexity)
{
[CmdletBinding()]
$ValidData = $true # the tests will set this to false if we find incorrect parameter.
#Check if parameter is an Integer
if( $Length -isnot [int])
{
Write-Host "Password Length must be a whole number of 8 or higher."
$ValidData = $false
}
#Check if parameter is 8 or higher
Elseif($length -le 7)
{
Write-Host "Password Length needs to be a value of 8 or more. "
$ValidData = $false
}
#Check if parameter is an Integer
if( $complexity -isnot [int])
{
Write-Host "Complexity value must be a whole number between 1 and 3.
1 = Upper Case + Lower Case Alpha Only
2 = Upper Case + Lower Case Alpha + Number
3 = Upper Case + Lower Case Alpha + Number + Special Characters
"
$ValidData = $false
}
#Check if parameter is between 1..3
elseif( $complexity -cnotin (1..3))
{
Write-Host "Complexity value must be a whole number between 1 and 3.
1 = Upper Case + Lower Case Alpha Only
2 = Upper Case + Lower Case Alpha + Number
3 = Upper Case + Lower Case Alpha + Number + Special Characters
"
$ValidData = $false
}
#if it passes the parameter checks run the script
if ($ValidData)
{
$Passwrd = @()
$Passwrd += (48..57) | Get-Random -Count 1
$Passwrd += (65..90) | Get-Random -Count 1
$passwrdf = (48..57) + (65..90)
if ($complexity -in (2..3)) {
$Passwrd += (97..122) | Get-Random -Count 1
$passwrdf = (48..57) + (65..90) + (97..122)
}
if ($complexity -eq 3) {
$Passwrd += (33..33) + (35..37) | Get-Random -Count 1
$passwrdf = (33..33) + (35..37) + (48..57) + (65..90) + (97..122)
}
$Passwrd += ( $passwrdf| Get-Random -Count ($Length - ($complexity + 1 )))
$Password = -join ($Passwrd | Get-Random -Count ([int]::MaxValue) | ForEach-Object { [char]$_ } )
return [String]$Password
}
else
#if parameter failed checks then let user know to rerun the script.
{
Write-Host "Please rerun this function with correct parameter."
return
}
}
get-password 10 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment