Home > Software design >  Powershell scrypt for generating
Powershell scrypt for generating

Time:09-25

I have a question regarding the script. I found a script that generates a password, but how to do it so that, for example, you can set the length of the password and probably some characters when generating, if you do not set it only after that, let it generate a default password, for some length

function Get-RandomCharacters($length, $characters) {
    $random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
    $private:ofs=""
    return [String]$characters[$random]
}
 
function Scramble-String([string]$inputString){     
    $characterArray = $inputString.ToCharArray()   
    $scrambledStringArray = $characterArray | Get-Random -Count $characterArray.Length     
    $outputString = -join $scrambledStringArray
    return $outputString 
}
 
$password = Get-RandomCharacters -length 5 -characters 'abcdefghiklmnoprstuvwxyz'
$password  = Get-RandomCharacters -length 1 -characters 'ABCDEFGHKLMNOPRSTUVWXYZ'
$password  = Get-RandomCharacters -length 1 -characters '1234567890'
$password  = Get-RandomCharacters -length 1 -characters '!"§$%&/()=?}][{@#* '
 
Write-Host $password
 
$password = Scramble-String $password
 
Write-Host $password

CodePudding user response:

replace line

function Get-RandomCharacters($length, $characters) {

with:

function Get-RandomCharacters($length = 8, $characters = 'abcdefghiklmnoprstuvwxyzABCDEFGHKLMNOPRSTUVWXYZ1234567890') {

it defines default length to 8 chars, and charset as abcdefghiklmnoprstuvwxyzABCDEFGHKLMNOPRSTUVWXYZ1234567890

CodePudding user response:

There are more ways than one to generate a password.

For instance this one:

function New-Password {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(8, 128)]
        [int]$TotalLength = 10,
        [int]$Digits = 3,
        [int]$Symbols = 2
    )
    # the number of symbols must be => 0 and <= $TotalLength
    $Symbols = [math]::Max(0,[math]::Min($Symbols, $TotalLength))
    # same for the number of digits
    $Digits = [math]::Max(0,[math]::Min($Digits, $TotalLength))
    $alphas = $TotalLength - ($Digits   $Symbols) 
    if ($alphas -lt 0) {
        throw "Too many digits or symbols for the total password length"
    }
    $list = [System.Collections.Generic.List[char]]::new()

    if ($Digits -gt 0)  { $list.AddRange([char[]]([char[]]'0123456789' | Get-Random -Count $Digits)) }
    if ($Symbols -gt 0) { $list.AddRange([char[]]([char[]]'!@#$%^&*()_- =[{]};:<>|./?~' | Get-Random -Count $Symbols)) }
    if ($alphas -gt 0)  { $list.AddRange([char[]]([char[]]'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | Get-Random -Count $alphas)) }

    ($list | Sort-Object {Get-Random}) -join ''
}

Usage:

$password = New-Password -TotalLength 12 -Symbols 4 -Digits 2  # or without parameters to accept the defaults

Or this:

$TotalLength = 10
$password = ([char[]]([char]33..[char]95)   ([char[]]([char]97..[char]126))   0..9 | Sort-Object {Get-Random})[0..$TotalLength] -join ''

Or this:

Add-Type -AssemblyName System.Web
# the first parameter specifies the total password length. 
# the second one specifies the minimum number of non-alphanumeric characters
$password = [System.Web.Security.Membership]::GeneratePassword(10, 3)

Note: Apparently you cannot use this last method in .NET Core as it does not support System.Web.dll. See this github issue

  • Related