I have a PowerShell script that when I run it, create a local amdmistrator on the computers that I select and it gives me another txt in which they were created.
But from the txt where the host collects only picks the last line and I do not know why.
Thanks for the help
cls
$username = "username"
$password = "password"
$computernames = get-content "C:\serverlist.txt"
foreach ($computername in $computernames) {
get-adcomputer $computername | select DistinguishedName > C:\serverlist2.txt
}
$users = $null
$computer = [ADSI]"WinNT://$computername"
Try {
$users = $computer.psbase.children | select -expand name
if ($users -like $username) {
Write-Host "$username already exists"
} Else {
$user_obj = $computer.Create("user", "$username")
$user_obj.SetPassword($password)
$user_obj.SetInfo()
$user_obj.Put("description", "$username")
$user_obj.SetInfo()
$user_obj.psbase.invokeset("AccountDisabled", "False")
$user_obj.SetInfo()
$users = $computer.psbase.children | select -expand name
if ($users -like $username) {
Write-Host "$username has been created on $($computer.name)"
$group = [ADSI]("WinNT://"+$computername+"/administradores,group")
$group.add("WinNT://"+$computername+"/"+$username+",user")
} Else {
Write-Host "$username has not been created on $($computer.name)"
}
}
} Catch {
Write-Host "Error creating $username on $($computer.path): $($Error[0].Exception.Message)"
}