Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve password generation function #69

Open
NickolajA opened this issue Feb 20, 2023 · 1 comment
Open

Improve password generation function #69

NickolajA opened this issue Feb 20, 2023 · 1 comment
Assignees
Labels

Comments

@NickolajA
Copy link
Member

function New-RandomPassword {
 # Creadit to https://arminreiter.com/2021/07/3-ways-to-generate-passwords-in-powershell/
    param (
        [Parameter(Mandatory)]
        [ValidateRange(4,[int]::MaxValue)]
        [int] $PasswordLength,
        [int] $upper = 1,
        [int] $lower = 1,
        [int] $numeric = 1,
        [int] $special = 1
    )
    if($upper + $lower + $numeric + $special -gt $PasswordLength) {
        throw "number of upper/lower/numeric/special char must be lower or equal to length"
    }

    $PasswordAllowedUpperCharacters = if (-not([string]::IsNullOrEmpty($env:PasswordAllowedUpperCharacters))) { $env:PasswordAllowedUpperCharacters } else { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }
    $PasswordAllowedLowerCharacters = if (-not([string]::IsNullOrEmpty($env:PasswordAllowedLowerCharacters))) { $env:PasswordAllowedLowerCharacters } else { "abcdefghijklmnopqrstuvwxyz" }
    $PasswordAllowedNumberCharacters = if (-not([string]::IsNullOrEmpty($env:PasswordAllowedNumberCharacters))) { $env:PasswordAllowedNumberCharacters } else { "123456789" }
    $PasswordAllowedSpecialCharacters = if (-not([string]::IsNullOrEmpty($env:PasswordAllowedSpecialCharacters))) { $env:PasswordAllowedSpecialCharacters } else { ".:;,-_!?$%*=+&<>@#()" }

    $charSet = ""
    if($upper -gt 0) { $charSet += $PasswordAllowedUpperCharacters }
    if($lower -gt 0) { $charSet += $PasswordAllowedLowerCharacters }
    if($numeric -gt 0) { $charSet += $PasswordAllowedNumberCharacters }
    if($special -gt 0) { $charSet += $PasswordAllowedSpecialCharacters }
    
    $charSet = $charSet.ToCharArray()
    $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
    $bytes = New-Object byte[]($PasswordLength)
    $rng.GetBytes($bytes)
 
    $result = New-Object char[]($PasswordLength)
    for ($i = 0 ; $i -lt $PasswordLength ; $i++) {
        $result[$i] = $charSet[$bytes[$i] % $charSet.Length]
    }
    $password = (-join $result)
    $valid = $true
    if($upper   -gt ($password.ToCharArray() | Where-Object {$_ -cin $PasswordAllowedUpperCharacters.ToCharArray() }).Count) { $valid = $false }
    if($lower   -gt ($password.ToCharArray() | Where-Object {$_ -cin $PasswordAllowedLowerCharacters.ToCharArray() }).Count) { $valid = $false }
    if($numeric -gt ($password.ToCharArray() | Where-Object {$_ -cin $PasswordAllowedNumberCharacters.ToCharArray() }).Count) { $valid = $false }
    if($special -gt ($password.ToCharArray() | Where-Object {$_ -cin $PasswordAllowedSpecialCharacters.ToCharArray() }).Count) { $valid = $false }
 
    if(!$valid) {
         $password = New-RandomPassword $PasswordLength $upper $lower $numeric $special
    }
    return $password
}
@NickolajA NickolajA self-assigned this Feb 20, 2023
@jayslayY
Copy link

jayslayY commented Mar 2, 2023

Hi Nickolaj =) I am currently running you solution on several thousand devices. Thanks!
Is the script above finished and in witch part of the solution will this code be inserted?

Is it code from the remidiation script?
Was just thinking if this is possible to test now by manually changing the code.
Looking forward to next version.

cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants