powershell 创建一个简单的脚本来导入用户的名字、创建本地帐户和密码

gfttwv5a  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(124)

我希望创建一个简单的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!!"

见上面的脚本......

xj3cbfub

xj3cbfub1#

我想你可以简单地用try/Catch-例如:

try {
    Set-LocalUser -Name $firstname -Password $password -Erroraction:stop
    write-host "Password updated successfully!!" 
}
Catch {
    write-error $_
}

如果操作成功,则返回密码更新成功!!,否则返回错误。

相关问题