Created
June 2, 2021 05:08
-
-
Save doggy8088/4bf8a3189460071acece2518f1fa01e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-RandomPassword { | |
param( | |
[Parameter()] | |
[int]$MinimumPasswordLength = 8, | |
[Parameter()] | |
[int]$MaximumPasswordLength = 12, | |
[Parameter()] | |
[switch]$ConvertToSecureString | |
) | |
$length = Get-Random -Minimum $MinimumPasswordLength -Maximum $MaximumPasswordLength | |
$password = ("!@#$%^&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray() | sort {Get-Random})[0..$length] -join '' | |
if ($ConvertToSecureString.IsPresent) { | |
ConvertTo-SecureString -String $password -AsPlainText -Force | |
} else { | |
$password | |
} | |
} | |
function GeneratePassword { | |
param( | |
[Parameter()] | |
[int]$MinimumPasswordLength = 8, | |
[Parameter()] | |
[int]$MaximumPasswordLength = 12, | |
[Parameter()] | |
[int]$NumberOfAlphaNumericCharacters = 5, | |
[Parameter()] | |
[switch]$ConvertToSecureString | |
) | |
Add-Type -AssemblyName 'System.Web' | |
$length = Get-Random -Minimum $MinimumPasswordLength -Maximum $MaximumPasswordLength | |
$password = [System.Web.Security.Membership]::GeneratePassword($length,$NumberOfAlphaNumericCharacters) | |
if ($ConvertToSecureString.IsPresent) { | |
ConvertTo-SecureString -String $password -AsPlainText -Force | |
} else { | |
$password | |
} | |
} | |
function New-Password { | |
<# | |
.SYNOPSIS | |
Generate a random password. | |
.DESCRIPTION | |
Generate a random password. | |
.NOTES | |
Change log: | |
27/11/2017 - faustonascimento - Swapped Get-Random for System.Random. | |
Swapped Sort-Object for Fisher-Yates shuffle. | |
17/03/2017 - Chris Dent - Created. | |
#> | |
[CmdletBinding()] | |
[OutputType([String])] | |
param ( | |
# The length of the password which should be created. | |
[Parameter(ValueFromPipeline)] | |
[ValidateRange(8, 255)] | |
[Int32]$Length = 10, | |
# The character sets the password may contain. A password will contain at least one of each of the characters. | |
[String[]]$CharacterSet = ('abcdefghijklmnopqrstuvwxyz', | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
'0123456789', | |
'!$%&^.#;'), | |
# The number of characters to select from each character set. | |
[Int32[]]$CharacterSetCount = (@(1) * $CharacterSet.Count), | |
[Parameter()] | |
[switch]$ConvertToSecureString | |
) | |
begin { | |
$bytes = [Byte[]]::new(4) | |
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() | |
$rng.GetBytes($bytes) | |
$seed = [System.BitConverter]::ToInt32($bytes, 0) | |
$rnd = [Random]::new($seed) | |
if ($CharacterSet.Count -ne $CharacterSetCount.Count) { | |
throw "The number of items in -CharacterSet needs to match the number of items in -CharacterSetCount" | |
} | |
$allCharacterSets = [String]::Concat($CharacterSet) | |
} | |
process { | |
try { | |
$requiredCharLength = 0 | |
foreach ($i in $CharacterSetCount) { | |
$requiredCharLength += $i | |
} | |
if ($requiredCharLength -gt $Length) { | |
throw "The sum of characters specified by CharacterSetCount is higher than the desired password length" | |
} | |
$password = [Char[]]::new($Length) | |
$index = 0 | |
for ($i = 0; $i -lt $CharacterSet.Count; $i++) { | |
for ($j = 0; $j -lt $CharacterSetCount[$i]; $j++) { | |
$password[$index++] = $CharacterSet[$i][$rnd.Next($CharacterSet[$i].Length)] | |
} | |
} | |
for ($i = $index; $i -lt $Length; $i++) { | |
$password[$index++] = $allCharacterSets[$rnd.Next($allCharacterSets.Length)] | |
} | |
# Fisher-Yates shuffle | |
for ($i = $Length; $i -gt 0; $i--) { | |
$n = $i - 1 | |
$m = $rnd.Next($i) | |
$j = $password[$m] | |
$password[$m] = $password[$n] | |
$password[$n] = $j | |
} | |
$password = [String]::new($password) | |
if ($ConvertToSecureString.IsPresent) { | |
ConvertTo-SecureString -String $password -AsPlainText -Force | |
} else { | |
$password | |
} | |
} catch { | |
Write-Error -ErrorRecord $_ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment