Skip to content

Instantly share code, notes, and snippets.

@rleap-m
Created September 18, 2020 20:27
Show Gist options
  • Save rleap-m/f70736c4cb29dc3d37d66472af66e013 to your computer and use it in GitHub Desktop.
Save rleap-m/f70736c4cb29dc3d37d66472af66e013 to your computer and use it in GitHub Desktop.
<#
.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