Public/New-RandomPassword.ps1
function New-RandomPassword { <# .Synopsis Генерирует случайный пароль. .Description Генерирует случайный пароль с заданной длиной (от 4 до 20 символов). .Parameter PasswordLength Определяет длину пароля (по-умолчанию - 15 символов) .Parameter Strong Добавляет к паролу символы с кодами 33, 35, 64 и 94 .Example New-RandomPassword -PasswordLength 4 .Example New-RandomPassword -PasswordLength 15 -Strong .Notes .Inputs Длина пароля и "надежность" .Outputs Пароль (строка) #> [CmdletBinding()] [OutputType([System.String])] Param( [Parameter(Mandatory = $false, ValueFromPipeline = $true, Position = 0)] [ValidateRange(4, 20)] [System.Int32] $PasswordLength = 15, [Parameter(Mandatory = $false, ValueFromPipeline = $true, Position = 1)] [Switch] $Strong ) Begin { } Process { try { $numberchars = 0..9 | ForEach-Object -Process {$PSItem.ToString()} $lochars = [System.Char]'a' .. [System.Char]'z' | ForEach-Object -Process {[System.Char]$PSItem} $hichars = [System.Char]'A' .. [System.Char]'Z' | ForEach-Object -Process {[System.Char]$PSItem} $punctchars = [System.Char[]](33, 35, 64, 94) $PasswordArray = Get-Random -InputObject @($hichars + $lochars + $numberchars) -Count $PasswordLength $char1 = Get-Random -InputObject $hichars $char2 = Get-Random -InputObject $lochars $char3 = Get-Random -InputObject $numberchars $RndIndexArray = Get-Random (0..($PasswordLength - 1)) -Count 4 $PasswordArray[$RndIndexArray[0]] = $char1 $PasswordArray[$RndIndexArray[1]] = $char2 $PasswordArray[$RndIndexArray[2]] = $char3 if ($Strong -eq $true) { $char4 = Get-Random -InputObject $punctchars $PasswordArray[$RndIndexArray[3]] = $char4 } } catch { $PSCmdlet.ThrowTerminatingError($PSitem) } } End { return [System.String]::Join('', $PasswordArray) } } |