读取 .txt powershell 的所有行

读取 .txt powershell 的所有行

我有一个 PowerShell 脚本,当我运行它时,它会在我选择的计算机上创建一个本地 amdmistrator,并为我提供另一个创建它们的 txt。

但是从主机收集的txt中只选取最后一行,我不知道为什么。

谢谢您的帮助

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

您的错误是因为在处理主要操作之前 foreach 循环已关闭:

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

当脚本继续执行时,只读取最后一个值$计算机名

您将修改脚本:

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

并修复一些缩进的错误。

相关内容