如何在 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 密碼免遭洩露。不要忘記關注我們並分享這篇文章。