将计算机添加到LogonWorkstation以获取用户列表

时间:2016-06-15 11:58:36

标签: powershell active-directory

我希望将两个工作站添加到用户列表中。 用户列表为accounts.txt,工作站为server01server02

我正在使用win 7计算机运行此脚本,我没有域管理员权限,但我确实有权将工作站手动添加到AD中的每个用户。

PS版本是4

ForEach ($user in $(Get-Content C:\Users\myusername\Documents\accounts.txt))
    { 
         $user.LogonWorkstations += ",server01, server02" 
         Set-ADUser -instance $user
    }

完整错误是:

The property 'LogonWorkstations' cannot be found on this object. Verify that the property exists and can be set.
At line:3 char:6
+      $user.LogonWorkstations += ",server01, server02"
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Set-ADUser : The instance parameter object must be of type: 'Microsoft.ActiveDirectory.Management.ADUser'.
At line:4 char:6
+      Set-ADUser -instance $user
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ArgumentException
    + FullyQualifiedErrorId : The instance parameter object must be of type: 'Microsoft.ActiveDirectory.Management.ADUser'.,Microsoft.ActiveDirectory.Manage 
   ment.Commands.SetADUser

1 个答案:

答案 0 :(得分:1)

您正尝试访问您从文件中读取的用户名字符串上的LogonWorkstations,但这不起作用。相反,您必须使用可以访问该属性的Get-AdUser cmdlet检索ADUser

尝试这样的事情(未经测试):

$users = Get-Content "C:\Users\myusername\Documents\accounts.txt"
ForEach ($user in $users)
{ 
    $adUseruser = Get-ADUser $user -Properties *
    $adUseruser.LogonWorkstations = @($adUseruser.LogonWorkstations, 'server01', 'server02') -join ','
    Set-ADUser -instance $adUseruser 
}