Powershell

閱讀 .txt powershell 的所有行

  • January 15, 2018

我有一個 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)"
}

您的錯誤是因為在處理主要操作之前 foreach 循環已關閉:

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

當腳本繼續執行時,只讀取**$computername**的最後一個值。

您將修改以下腳本:

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

並修正一些縮進的錯誤。

引用自:https://serverfault.com/questions/892181