当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 PowerShell 中生成安全随机密码

作者:精品下载站 日期:2024-12-14 01:56:29 浏览:15 分类:玩电脑

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


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

开始之前

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

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

  1. Get-Random - Get-Random cmdlet 仅适用于 Windows PowerShell 5.1,更重要的是它不能确保加密安全的随机性。摆脱它!
  2. Get-SecureRandom - Get-SecureRandom cmdlet 仅适用于 PowerShell 7.x。这是一个优秀的 cmdlet。不过,对于想要在 Windows PowerShell 5.1 上运行它的用户来说,它不向后兼容。
  3. RNGCryptoServiceProvider - RNGCryptoServiceProvider 抽象类已过时。所以你不应该在任何地方使用它。摆脱它!
  4. RandomNumberGenerator - RandomNumberGenerator 抽象类是安全的。优点是它适用于 Windows PowerShell 5.1 和 PowerShell 7.x。

因此,我建议使用 RandomNumberGenerator 在 PowerShell 中生成安全的随机密码。

生成随机安全密码 Powershell 脚本

下载 Get-RandomPassword.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。

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

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Get-RandomPassword.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .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.a-d.site/generate-secure-random-passwords-powershell/

    .NOTES
    Written by: ALI TAJRAN
    Website:    a-d.site
    X:          x.com/a-d
    LinkedIn:   linkedin.com/in/a-d

    .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 示例。

生成安全随机密码 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 密码免遭泄露。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯