0

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)"
}

1 Answers1

0

Your error was because the foreach loop was closed before you process the main action:

$computernames = get-content "C:\serverlist.txt"
     foreach ($computername in $computernames) {
     get-adcomputer $computername | select DistinguishedName >    C:\serverlist2.txt
} # <-- This is the error

And when the script continue executing, read only the last value of $computername.

You'll modify the script that:

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)"
    }
}

And fix some mistakes on indentation.

Zoredache
  • 130,897
  • 41
  • 276
  • 420
Victor Silva
  • 116
  • 3