Created
September 18, 2020 20:27
-
-
Save rleap-m/f70736c4cb29dc3d37d66472af66e013 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
<# | |
.SYNOPSIS | |
Generates a password | |
.PARAMETER Length | |
Number of characters that should be in the password | |
.PARAMETER NonAlphaMinCount | |
The minimum number of non-alphanumeric characters (such as @, #, !, %, &, and so on) in the generated password | |
.NOTES | |
[email protected] | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(1,128)] | |
[int] $Length = 15, | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(0,8)] | |
[int] $NonAlphaMinCount = 2 | |
) | |
$assemblyName = 'System.Web' | |
Try { | |
Add-Type -AssemblyName $assemblyName -ErrorAction Stop | |
if ('System.Web.Security.Membership' -as [type]) { | |
[System.Web.Security.Membership]::GeneratePassword($Length,$NonAlphaMinCount) | |
} | |
else { | |
Write-Warning '[GeneratePassword] method not available (requires full .NET CLR). Cannot create new password.' | |
} | |
} | |
Catch { | |
Write-Warning "Unable to load required assembly [$assemblyName]." | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment