Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在使用Where-Object之后使用Get-随机创建一个空值表达式。

在使用Where-Object之后使用Get-随机创建一个空值表达式。
EN

Stack Overflow用户
提问于 2018-02-28 20:53:39
回答 6查看 315关注 0票数 1

我正在尝试创建一个包含数字、字母(大写和小写)和符号的10个字符密码。

下面是我在函数中使用的脚本:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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对象排除列表中添加还是删除字符。

我已经在网上寻找信息,似乎找不到任何信息,虽然这可能是我正在使用的搜索词,因为这是一个有点复杂的情况,不会有多少人会报道。

任何帮助都是非常感谢的。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2018-02-28 21:18:03

使用下面的脚本似乎完全符合我的要求。

在这行的+=之后,我删除了逗号(,):

$ascii = $NULL;For ($a = 33;$a –le 126;$a++) {$ascii +=, ([char][byte]$a | Where-Object {$_ -notin "'",'`','|','_',";",'"',','})}

在将数组添加到:$ascii = @()之前,我创建了一个空白数组。

完整的代码块如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2018-02-28 21:12:18

简短的版本(对我来说是最好的方法):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$possible=36..38 + 40..43 + 45..58 + 60..94 + 97..123 + 125..126 + 33
(get-random -count 10 -input $possible | % {[char]$_}) -join ''
票数 1
EN

Stack Overflow用户

发布于 2018-02-28 21:48:25

我没有写这个,我也不记得是从哪里得到的,但是我已经将它内置到任何脚本中来创建随机安全的[int]$PasswordLength密码,您可以指定param [int]$PasswordLength返回的密码的长度(我已经将它设置为10 )。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

剧本可以在这里找到。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49042848

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文