PowerShell-密码生成器-如何始终在字符串中包含数字? [英] PowerShell - Password Generator - How to always include number in string?

查看:156
本文介绍了PowerShell-密码生成器-如何始终在字符串中包含数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PowerShell脚本,该脚本创建了一个15位随机字符串,用作Active Directory密码.

I have the following PowerShell script that creates a random string of 15 digits, for use as an Active Directory password.

麻烦的是,这在大多数情况下都有效,但是在某些情况下,它不使用数字或符号.我刚收到15封信.然后,该密码不能用作Active Directory密码,因为它必须至少包含一个数字或符号.

The trouble is, this works great most of the time, but on some occasions it doesn't use a number or symbol. I just get 15 letters. This is then not usable as an Active Directory password, as it must have at least one number or symbol in it.

$punc = 46..46
$digits = 48..57
$letters = 65..90 + 97..122
$YouShallNotPass = get-random -count 15 `
-input ($punc + $digits + $letters) |
% -begin { $aa = $null } `
-process {$aa += [char]$_} `
-end {$aa}

Write-Host "Password is $YouShallNotPass"

如何修改脚本中始终至少包含一个随机数或符号?

How would I amend the script to always have at least one random number or symbol in it?

谢谢.

推荐答案

您可以调用获取-Random cmdlet三次,每次使用不同的input参数(标点,数字和字母),合并结果字符串并使用另一个Get-Random调用对其进行混洗:

You could invoke the Get-Random cmdlet three times, each time with a different input parameter (punc, digit and letters), concat the result strings and shuffle them using another Get-Random invoke:

 (Get-Random -Count 15 -InputObject ([char[]]$yourPassword)) -join ''

但是,为什么要重新发明轮子呢?考虑使用以下 GeneratePassword 函数:

However, why do you want to reinvent the wheel? Consider using the following GeneratePassword function:

[Reflection.Assembly]::LoadWithPartialName("System.Web")
[System.Web.Security.Membership]::GeneratePassword(15,2)

并确保至少包含一个随机数(您已经指定了符号数):

And to ensure, it contains at least one random number (you already specify the number of symbols):

do {
   $pwd = [System.Web.Security.Membership]::GeneratePassword(15,2)
} until ($pwd -match '\d')

这篇关于PowerShell-密码生成器-如何始终在字符串中包含数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆