如何在 PowerShell 中生成安全随机密码

Jacki

有大量脚本和 cmdlet 可在 PowerShell 中生成密码。不幸的是,它们中的大多数都不安全,您不应该使用它们。那么,让我们看看它们,看看您应该使用哪一个。在本文中,您将了解如何在 PowerShell 中生成安全的随机密码。

开始之前

在 PowerShell 中生成随机密码的方法有很多种。然而,大多数人不知道大多数脚本或命令实际上不会生成安全密码,因此您不应该使用它们!

让我们看看最流行的密码随机生成器 cmdlet 和类:

  1. 随机获取- 这随机获取cmdlet 仅适用于 Windows PowerShell 5.1,更重要的是它不能确保加密安全的随机性。摆脱它!
  2. 获取安全随机- 这获取安全随机cmdlet 仅适用于 PowerShell 7.x。这是一个优秀的 cmdlet。不过,对于想要在 Windows PowerShell 5.1 上运行它的用户来说,它不向后兼容。
  3. RNG加密服务提供商- 这RNG加密服务提供商抽象类已经过时了。所以你不应该在任何地方使用它。摆脱它!
  4. 随机数生成器- 这随机数生成器抽象类是安全的。优点是它适用于 Windows PowerShell 5.1 和 PowerShell 7.x。

所以,我建议使用随机数生成器在 PowerShell 中生成安全的随机密码。

下载 Get-RandomPassword.ps1 PowerShell 脚本并将其放入C:脚本文件夹。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。给它起个名字获取随机密码.ps1并将其放置在C:脚本文件夹。

<#
    .SYNOPSIS
    Get-RandomPassword.ps1

    .DESCRIPTION
   This script generates passwords based on user-defined criteria for length, character
   sets, and quantity. It ensures each password contains at least one character from
   each specified set.

   .LINK
    www.alitajran.com/generate-secure-random-passwords-powershell/

    .NOTES
    Written by: ALI TAJRAN
    Website:    alitajran.com
    X:          x.com/alitajran
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 10/12/2024 - Initial version
#>

param (
    # The length of each password which should be created.
    [Parameter(Mandatory = $true)]
    [ValidateRange(8, 255)]
    [Int32]$Length,

    # The number of passwords to generate.
    [Parameter(Mandatory = $false)]
    [Int32]$Count = 1,

    # The character sets the password may contain.
    # A password will contain at least one of each of the characters.
    [String[]]$CharacterSet = @(
        'abcdefghijklmnopqrstuvwxyz',
        'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        '0123456789',
        '!$%&^.#;'
    )
)

# Generate a cryptographically secure seed
$bytes = [Byte[]]::new(4)
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
$seed = [System.BitConverter]::ToInt32($bytes, 0)
$rnd = [Random]::new($seed)

# Combine all character sets for random selection
$allCharacterSets = [String]::Concat($CharacterSet)

try {
    for ($i = 0; $i -lt $Count; $i++) {
        $password = [Char[]]::new($Length)
        $index = 0

        # Ensure at least one character from each set
        foreach ($set in $CharacterSet) {
            $password[$index++] = $set[$rnd.Next($set.Length)]
        }

        # Fill remaining characters randomly from all sets
        for ($j = $index; $j -lt $Length; $j++) {
            $password[$index++] = $allCharacterSets[$rnd.Next($allCharacterSets.Length)]
        }

        # Fisher-Yates shuffle for randomness
        for ($j = $Length - 1; $j -gt 0; $j--) {
            $m = $rnd.Next($j + 1)
            $t = $password[$j]
            $password[$j] = $password[$m]
            $password[$m] = $t
        }

        # Output each password
            Write-Output ([String]::new($password))
    }
}
catch {
    Write-Host "Error: $_" -ForegroundColor Red
}

现在一切就绪,让我们看一下用于生成安全密码的 PowerShell 示例。

了解更多:如何查看 iPhone 上保存的密码并将其导出

生成安全随机密码 PowerShell 脚本示例

生成字符长度为 20 的安全随机密码。

C:scripts.Get-RandomPassword.ps1 -Length "20"

生成 100 个字符长度为 20 的安全随机密码。

C:scripts.Get-RandomPassword.ps1 -Length "20" -Count "100"

生成每个字符集至少包含 1 个字符的安全随机密码。

C:scripts.Get-RandomPassword.ps1 -Length 20 -CharacterSet "ABCDEF", "1234567890", "!@#$%^&*()"

如果您有要生成随机密码的 PowerShell 脚本,请将此脚本用作函数。这里有两篇文章就是这样做的。

  • 强制更改 Microsoft 365 中所有用户的密码
  • 使用随机密码批量创建 AD 用户

结论

您学习了如何在 PowerShell 中生成安全的随机密码。如果您确定只想使用 PowerShell 7,请在 PowerShell 中使用 Get-SecureRandom cmdlet。如果您想在所有 PowerShell 版本中生成密码,最好使用 RandomNumberGenerator 抽象类。这就是我喜欢使用和推荐的。

您喜欢这篇文章吗?您可能还喜欢如何保护 Active Directory 密码免遭泄露。不要忘记关注我们并分享这篇文章。