我正在尝试创建一个包含数字、字母(大写和小写)和符号的10个字符密码。
下面是我在函数中使用的脚本:
Function Get-TempPassword {
$TempPassword = $null
$ascii = $NULL;For ($a = 33;$a –le 126;$a++) {$ascii +=, ([char][byte]$a | Where-Object {$_ -notin "'",'`','|','_',"`;",'"',','})}
Do {$TempPassword += $ascii | Get-Random; $loop++}
Until ($loop -eq 11)
return $TempPassword
}
如果删除以下部分:
{$_ -notin“‘’、‘\”、'_’、‘_’、“”、‘’、‘’、}}
密码的创建工作良好,尽管包括我不希望包含的符号。
具有Where-Object函数会导致get -随机函数只使用数组中的前5个字符,因此我不会收到任何大小写类型的字母、数字或任何其他符号。
我发现如果我使用$ascii[26]
(是数组中的第25个字符),我会得到一个空值,但是我认为这将允许使用任何字符,或者根本不使用该字符,而不仅仅是前5个字符。第25个字符恰好是一个;(ascii值为59)。我尝试将符号添加到Where对象排除项中,并将其从数组中删除,但第25个字符仍然显示为空值。
我执行了对每个字符的ascii值[int[]][char[]]
的反向查找,每个字符的两侧都将出现;符号并返回值58和60,这使我相信是值59违规,但此时的符号应该被排除在外。
将字符添加到“Where-Object”排除列表中应该会将它们从数组中移除,但是运行$ascii.Count
会显示49个字符,而不管我是向Where对象排除列表中添加还是删除字符。
我已经在网上寻找信息,似乎找不到任何信息,虽然这可能是我正在使用的搜索词,因为这是一个有点复杂的情况,不会有多少人会报道。
任何帮助都是非常感谢的。
发布于 2018-02-28 21:18:03
使用下面的脚本似乎完全符合我的要求。
在这行的+=之后,我删除了逗号(,):
$ascii = $NULL;For ($a = 33;$a –le 126;$a++) {$ascii +=, ([char][byte]$a | Where-Object {$_ -notin "'",'`','|','_',";",'"',','})}
在将数组添加到:$ascii = @()
之前,我创建了一个空白数组。
完整的代码块如下:
Function Get-TempPassword {
$TempPassword = $null
$ascii = @()
For ($a = 33;$a –le 126; $a++) { $ascii += ([char][byte]$a | Where-Object { $_ -notin "'",'`','|','_',";",'"',',' }) }
Do {$TempPassword += $ascii | Get-Random; $loop++}
Until ($loop -eq 11)
return $TempPassword
}
发布于 2018-02-28 21:12:18
简短的版本(对我来说是最好的方法):
$possible=36..38 + 40..43 + 45..58 + 60..94 + 97..123 + 125..126 + 33
(get-random -count 10 -input $possible | % {[char]$_}) -join ''
发布于 2018-02-28 21:48:25
我没有写这个,我也不记得是从哪里得到的,但是我已经将它内置到任何脚本中来创建随机安全的[int]$PasswordLength
密码,您可以指定param [int]$PasswordLength
返回的密码的长度(我已经将它设置为10 )。
function New-SWRandomPassword {
[CmdletBinding(DefaultParameterSetName='FixedLength',ConfirmImpact='None')]
[OutputType([String])]
Param
(
# Specifies minimum password length
[Parameter(Mandatory=$false,
ParameterSetName='RandomLength')]
[ValidateScript({$_ -gt 0})]
[Alias('Min')]
[int]$MinPasswordLength = 8,
# Specifies maximum password length
[Parameter(Mandatory=$false,
ParameterSetName='RandomLength')]
[ValidateScript({
if($_ -ge $MinPasswordLength){$true}
else{Throw 'Max value cannot be lesser than min value.'}})]
[Alias('Max')]
[int]$MaxPasswordLength = 11,
# Specifies a fixed password length
[Parameter(Mandatory=$false,
ParameterSetName='FixedLength')]
[ValidateRange(1,2147483647)]
[int]$PasswordLength = 10,
# Specifies an array of strings containing charactergroups from which the password will be generated.
# At least one char from each group (string) will be used.
[String[]]$InputStrings = @('abcdefghijkmnpqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!"#%&'),
# Specifies a string containing a character group from which the first character in the password will be generated.
# Useful for systems which requires first char in password to be alphabetic.
[String] $FirstChar,
# Specifies number of passwords to generate.
[ValidateRange(1,2147483647)]
[int]$Count = 1
)
Begin {
Function Get-Seed{
# Generate a seed for randomization
$RandomBytes = New-Object -TypeName 'System.Byte[]' 4
$Random = New-Object -TypeName 'System.Security.Cryptography.RNGCryptoServiceProvider'
$Random.GetBytes($RandomBytes)
[BitConverter]::ToUInt32($RandomBytes, 0)
}
}
Process {
For($iteration = 1;$iteration -le $Count; $iteration++){
$Password = @{}
# Create char arrays containing groups of possible chars
[char[][]]$CharGroups = $InputStrings
# Create char array containing all chars
$AllChars = $CharGroups | ForEach-Object {[Char[]]$_}
# Set password length
if($PSCmdlet.ParameterSetName -eq 'RandomLength')
{
if($MinPasswordLength -eq $MaxPasswordLength) {
# If password length is set, use set length
$PasswordLength = $MinPasswordLength
}
else {
# Otherwise randomize password length
$PasswordLength = ((Get-Seed) % ($MaxPasswordLength + 1 - $MinPasswordLength)) + $MinPasswordLength
}
}
# If FirstChar is defined, randomize first char in password from that string.
if($PSBoundParameters.ContainsKey('FirstChar')){
$Password.Add(0,$FirstChar[((Get-Seed) % $FirstChar.Length)])
}
# Randomize one char from each group
Foreach($Group in $CharGroups) {
if($Password.Count -lt $PasswordLength) {
$Index = Get-Seed
While ($Password.ContainsKey($Index)){
$Index = Get-Seed
}
$Password.Add($Index,$Group[((Get-Seed) % $Group.Count)])
}
}
# Fill out with chars from $AllChars
for($i=$Password.Count;$i -lt $PasswordLength;$i++) {
$Index = Get-Seed
While ($Password.ContainsKey($Index)){
$Index = Get-Seed
}
$Password.Add($Index,$AllChars[((Get-Seed) % $AllChars.Count)])
}
Return $(-join ($Password.GetEnumerator() | Sort-Object -Property Name | Select-Object -ExpandProperty Value))
}
}
}
New-SWRandomPassword
编辑::
https://gallery.technet.microsoft.com/scriptcenter/Generate-a-random-and-5c879ed5
剧本可以在这里找到。
https://stackoverflow.com/questions/49042848
复制相似问题