我在powershell中创建了一个脚本来检查密码组合的强度我想添加一个用户可以从文本文件中输入密码的选项(用户输入-f,然后输入文件路径,然后他得到密码审查)或者他之前没有任何选项输入密码
如果我想使用论点,我想问我需要做什么。就像用户不使用文件来从文本文件中读取密码一样,他只需自己输入密码
Param([string]$f)
if($PSBoundParameters.Values.Count -eq 0)
{
#creating a variable password that will input the argument (user's input)
$password=$args[0]
}
else
{
$password = Get-Content $f
}
#evaluating how much chars the password has
$password_length=$password.length
#Creating an array for stroing reasons why the password is incorrect
$requirements = [System.Collections.ArrayList]::new()
#counter for checking in how much sections the password meets the rquirements
$count=0
#Checking if password includes minimum of 10 characters
if ($password_length -ge 10)
{
[void]$requirements.Add('Correct')
}
else
{
[void]$requirements.Add("Incorrect password syntax. The password
length must includes minimum of 10 characters").ToString()
}
#checkig if the password includes both alphabet and number
if ((($password -cmatch "[A-Z]") -or ($password -cmatch "[a-z]")) -and ($password -cmatch "[0-9]"))
{
[void]$requirements.Add('Correct')
}
else
{
[void]$requirements.Add("Incorrect password syntax. The password
must includes both alphabet and number")
}
#checking if password includes both the small and capital case letters.
if (($password -cmatch "[A-Z]") -and ($password -cmatch "[a-z]"))
{
[void]$requirements.Add('Correct')
}
else
{
[void]$requirements.Add("Incorrect password syntax. The password
must includes both the small and capital case letters")
}
#checking whether the password is according to the requirements or not
foreach ($requirement in $requirements)
{
if($requirement -eq "Correct")
{
$count++;
}
}
#if count is equal to 3 the password will be printed in green foreground color
if ($count -eq 3)
{
Write-Host "$password" -ForegroundColor Green
}
#if is not equal to 3 the password and the requirements that has to be fixed will be printed in red foreground color
else
{
Write-Host "$password" -ForegroundColor Red
foreach ($requirement in $requirements)
{
if ($requirement -ne "Correct")
{
Write-Host "$requirement" -ForegroundColor Red
}
}
}
我尝试使用 $PSBoundParameters.Valus.Count 并检查它是否等于 0 但我该如何从那里继续
我想达到的场景:
- 用户在没有文件的情况下运行脚本 — .\script.ps1 user_password
- 用户使用文件运行脚本 —
.\script.ps1 -f file name.txt
我想识别每个场景如果用户不使用文件,我将使用它作为输入示例——.\script.ps1 127373873Aa脚本将检查这个字符串,如果他使用文件,密码将从文件中变为红色,之后将被检查
你知道我该如何解决这个谢谢