【问题标题】:Local Admin password change throughout the domain整个域的本地管理员密码更改
【发布时间】: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


【解决方案1】:

$user 最初是一个包含用户名的字符串。在循环中,您使用 ADSI 对象覆盖它。然后在"WinNT://$server/$user,user" 字符串中再次使用它,但失败了。

只需在循环中为 $user 使用另一个名称就可以了。

编辑:将脚本更改为

$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{
        $adsiUser = [adsi]"WinNT://$server/$user,user"
        $adsiUser.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
    }

    $adsiUser.SetInfo()
}

【讨论】:

  • 它不起作用,我在循环中声明了 $user 和 $password 并且它起作用了。
猜你喜欢
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 2018-09-21
  • 2018-08-09
  • 2019-11-26
  • 2014-04-29
相关资源
最近更新 更多