有人可以帮助我如何才能搜索用户的所有AD,我不知道谁存在或不存在。
根域(NA1.local)
资源域(domain1.local,domain2.local,domain3.local)
MSmith (不确定他\她在域中的位置,也不确定用户AD是否已从AD中删除) $user = PSmith
foreach ($domain in $domains)
{
Get-ADUser -Identity $username -Server $domain -ErrorAction SilentlyContinue
if ($? -eq 'True') {
$forest = Get-ADUser $username -Server $domain
Add-ADGroupMember -Identity $GPName -Member $forest -Server $VbrickSrv }
}发布于 2019-05-03 00:44:17
使用-Filter或-LDAPFilter与-Identity参数指定用户名。过滤器通常更快,因为它们在DC上进行过滤,而不是在运行脚本的机器上进行本地过滤。此外,如果在使用筛选器(通过identity)参数时未返回任何内容,则cmdlet不会生成异常。
$user = Get-ADUser -Filter { SamAccountName -eq 'psmith' }
...我通常会告诉您将搜索基础设置为对您的搜索有意义的内容。限制越多越好,但我看到你想搜索整个域。把它放在脑后就行了。
请查看下面的文章以进行更深入的讨论。
发布于 2019-05-03 08:39:37
正如@Adam所说的使用筛选器而不是where子句(这是正确的答案),当您找到用户时,您可以简化您的代码
foreach ($domain in $domains) {
If ($u = Get-ADUser -Filter 'SamAccountName -eq "psmith"' -server $domain) {
Add-ADGroupMember -Identity $GPName -Members $u -Server $domain
Break #this should exit from the foreach loop
}
}https://stackoverflow.com/questions/55955920
复制相似问题