简体   繁体   English

如何获取AD中的employeeID attritubute计数,其中employeeID = X

[英]How to get count of employeeID attritubute in AD where employeeID = X

I'm working on writing a Powershell script that automatically adds new employees on a scheduled task from an automatically generated list that is created nightly. 我正在编写一个Powershell脚本,该脚本从每晚创建的自动生成的列表中自动将新员工添加到计划任务中。 One of the requirements, naturally, is to not create duplicate employees. 要求之一自然是不要创建重复的员工。 So, my thought process is to count the number of occurances of an employee ID, and if it is >= "1", then to not create the account and proceed to the next one. 因此,我的想法是计算一个员工ID的出现次数,如果> =“ 1”,则不创建该帐户并转到下一个帐户。 When trying to find the count i'm using the following code. 当试图找到计数时,我正在使用以下代码。 However, I'm consistently getting 0 as the return. 但是,我一直得到0作为回报。 Can someone correct my query for me. 有人可以为我更正我的查询吗? Thanks in advance. 提前致谢。

$ID = 12345
$COUNT = (Get-ADUser -filter * |where '$_.EmployeeID -eq $ID').count
Write-Output $COUNT

There are two things going wrong here. 这里有两件事出了错。 First, when you use -Filter * , it gets all accounts (up to 1000, unless you use the ResultPageSize parameter), loads them all into memory and passes the big list to the where . 首先,当您使用-Filter * ,它将获取所有帐户 (最多1000个,除非您使用ResultPageSize参数),然后将它们全部加载到内存中并将大列表传递到where That will take a whole lot longer and more memory than needed, and, if you have more than 1000 accounts in your domain, you won't get accurate results. 这将花费更长的时间和更多的内存,而不是所需的内存,并且,如果您的域中有1000个以上的帐户,您将无法获得准确的结果。

Second, Get-ADUser only returns a specific set of properties, unless you tell it otherwise. 其次,除非另有说明,否则Get-ADUser仅返回一组特定的属性。 Since you didn't tell it that you want employeeID , it didn't return it. 由于您没有告诉您需要employeeID ,因此它没有返回它。 So $_.EmployeeID will always be empty. 因此, $_.EmployeeID始终为空。

Both issues can be solved at the same time by passing your EmployeeID filter directly to Get-ADUser : 通过将EmployeeID过滤器直接传递给Get-ADUser可以同时解决这两个问题:

$ID = 12345
$COUNT = (Get-ADUser -filter 'EmployeeID -eq $ID').count
Write-Output $COUNT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM