Created
November 25, 2016 18:00
-
-
Save 9999years/7804a09c4a3e18a786507b66f5af6130 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
#Random string generation for passwords etc | |
#defaults to all printable ascii | |
#works either inclusively, building a character palette up from zero with switches like -Numbers | |
#or exclusively, starting with all printable ascii and removing characters, as with -NoNumbers | |
#`Random-String -Numbers $False` and `Random-String -NoNumbers` work the same | |
function Random-String { | |
[CmdletBinding()] | |
Param( | |
[Int]$Length = 16, | |
[Switch]$Letters, | |
[Switch]$NoLetters, | |
[Switch]$Symbols, | |
[Switch]$NoSymbols, | |
[Switch]$Numbers, | |
[Switch]$NoNumbers, | |
[Switch]$Alphanumeric, | |
[Switch]$Lowercase, | |
[Switch]$Uppercase | |
) | |
If($Length -eq 0) { | |
#????? | |
return | |
} | |
#No preferences | |
If(!$Letters -and | |
!$Symbols -and | |
!$Numbers -and | |
!$Alphanumeric -and | |
!$Lowercase -and !$Uppercase) { | |
#Defaults | |
$Letters = [Switch]::Present | |
$Symbols = [Switch]::Present | |
$Numbers = [Switch]::Present | |
} | |
If($Alphanumeric) { | |
$Letters = [Switch]::Present | |
$Numbers = [Switch]::Present | |
} | |
$Symbols = ($Symbols -and !$NoSymbols) | |
$Numbers = ($Numbers -and !$NoNumbers) | |
$Letters = ($Letters -and !$NoLetters) | |
$Palette = "" | |
If($Alphanumeric) { | |
$Letters = [Switch]::Present | |
$Numbers = [Switch]::Present | |
} | |
If($Letters) { | |
If($Lowercase -and !$Uppercase) { | |
$Palette += "abcdefghijklmnopqrstuvwxyz" | |
} ElseIf($Uppercase -and !$Lowercase) { | |
$Palette += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
} Else { | |
$Palette += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
} | |
} | |
If($Numbers) { $Palette += "0123456789" } | |
If($Symbols) { $Palette += "!`"#`$%&'()*+,-./:;<=>?@[\]^_``{|}~" } | |
0..($Length - 1) | %{ | |
$str += $Palette[(Get-Random -Maximum $Palette.Length)] | |
} | |
return $str | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment