我希望创建一个简单的PowerShell脚本,将从文件中导入用户的名字,提示创建一个新的密码,并循环在错误时,密码不符合基于“ErrorVariable”的密码要求,如果可能。如果没有,请给我建议。
# import user firstname from file
$firstname = $(Get-Content "C:\tmp\local\firstname.txt")
# prompt user for new password
$password = Read-Host "Hello, $firstname!! Please change your local admin account password. (Requirements: At least 8-characters, 1-Cap Letter, 1-Number) " -AsSecureString -Erroraction silentlycontinue -ErrorVariable PasswordError
# create new password
$password = $password
Get-LocalUser -Name "$firstname" | Set-LocalUser -Password $password -Erroraction silentlycontinue -ErrorVariable PasswordError
If ($PasswordError)
{
"Unable to update the password. The new password does not meet the length or complexity."
}
If (-Not $PasswordError)
{
"Password updated successfully!!"
见上面的脚本......
1条答案
按热度按时间xj3cbfub1#
我想你可以简单地用try/Catch-例如:
如果操作成功,则返回密码更新成功!!,否则返回错误。