Menu
CoddyTech

数字和大写字母

本课属于 Coddy PowerShell 学习旅程的“PowerShell 基础”单元。第 23 课,共 28 课。

challenge icon

挑战

简单

强密码混合了数字和大写字母。统计这两者的数量;保留你目前为止编写的所有代码。

  1. $pw.ToCharArray() 会将密码的字符作为数组返回。使用 foreach 循环遍历它。
  2. 对于数字,[char]::IsDigit($c) 为 True,对于大写字母,[char]::IsUpper($c) 为 True。统计每种字符的数量。
  3. 在长度行之后,输出 Digits: 和数字的数量,然后输出 Capitals: 和大写字母的数量。

当输入为 Sunflower42 时,你的脚本输出:

Password Checker
================
Password: Sunflower42
Characters: 11
Length: OK
Digits: 2
Capitals: 1

亲手试试

# Password Checker
Write-Output "Password Checker"
Write-Output "================"
$pw = Read-Host "Password"
Write-Output "Characters: $($pw.Length)"
if ($pw.Length -ge 8) {
    Write-Output "Length: OK"
} else {
    Write-Output "Length: too short (need 8)"
}

PowerShell 基础中的所有课