数字和大写字母
本课属于 Coddy PowerShell 学习旅程的“PowerShell 基础”单元。第 23 课,共 28 课。
挑战
简单强密码混合了数字和大写字母。统计这两者的数量;保留你目前为止编写的所有代码。
$pw.ToCharArray()会将密码的字符作为数组返回。使用foreach循环遍历它。- 对于数字,
[char]::IsDigit($c)为True,对于大写字母,[char]::IsUpper($c)为True。统计每种字符的数量。 - 在长度行之后,输出
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)"
}