【发布时间】:2017-02-22 02:33:58
【问题描述】:
我一直在尝试这个脚本来重置所有本地管理员密码,如果服务器列表中只有一台服务器,脚本运行得很好,但是,如果有多个服务器,它会抛出错误 -
server x "找不到用户名。" 检索成员“SetInfo”时发生以下异常:“用户 n 找不到ame。” 在 C:\Users\v7t7adm\desktop\localadmin.ps1:18 char:15 + $user.SetInfo
这是我创建的脚本 -
$computers = Get-Content servers.txt
"Name`tStatus" | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv
$user = "KArthur"
$password = "Pol!sh90&8"
foreach($server in $computers)
{
try{
$user = [adsi]"WinNT://$server/$user,user"
$user.SetPassword($password)
"$server`tSuccess"
"$server`tSuccess" | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv -Append
}
catch{
"$server`t" + $_.Exception.Message.ToString().Split(":")[1].Replace("`n","")
"$server`t" + $_.Exception.Message.ToString().Split(":")[1].Replace("`n","") | Out-File -FilePath C:\users\v7t7adm\desktop\results.csv -Append
}
$user.SetInfo()
}
【问题讨论】:
-
在脚本中硬编码纯文本密码是一个非常糟糕的主意。 (也不好发到网上给大家看。)
标签: powershell powershell-2.0 powershell-3.0 powershell-4.0 powershell-remoting