获取特定组织单位下的所有广告对象名称

huangapple go评论95阅读模式
英文:

Get all AD object name under specific OU

问题

我想修改下面的PowerShell脚本以导出OU成员(用户和计算机),其中输入将如下所示:

  1. $OUlist = @(
  2. domain.com/Site-A/OU1
  3. domain.com/Site-A/OU2
  4. domain.com/Site-B/OU1
  5. domain.com/Site-B/OU2
  6. ...
  7. domain.com/Site-Z/OU1
  8. domain.com/Site-Z/OU99
  9. )
  10. $targetOUs = $OUList
  11. $users = $targetOUs | ForEach-Object {
  12. Get-ADUser -Filter * -SearchBase $_.distinguishedName
  13. Get-ADComputer -Filter * -SearchBase $_.distinguishedName
  14. } | Export-CSV "Result.csv" -NTI

Result.CSV 内容:

  1. Name, Type
  2. PC1, Computer
  3. Person1, User
  4. PC2, Computer
  5. Person2, User
英文:

I would like to modify this PowerShell script below to export the OU members (Users and Computers) where the input will be like this:

  1. $OUlist = @(
  2. domain.com/Site-A/OU1
  3. domain.com/Site-A/OU2
  4. domain.com/Site-B/OU1
  5. domain.com/Site-B/OU2
  6. ...
  7. domain.com/Site-Z/OU1
  8. domain.com/Site-Z/OU99
  9. )
  10. $targetOUs = $OUList
  11. $users = $targetOUs |ForEach-Object {
  12. Get-ADUser -Filter * -SearchBase $_.distinguishedName
  13. Get-ADComputer -Filter * -SearchBase $_.distinguishedName
  14. } | Export-CSV "Result.csv" -NTI

Result.CSV content:

  1. Name, Type
  2. PC1, Computer
  3. Person1, User
  4. PC2, Computer
  5. Person2, User

答案1

得分: 1

自从你有一个 CanonicalNames 列表,那么你需要查询所有的组织单位 (OUs) 以正确获取 OU 的 DistinguishedName,以便将其用作 -SearchBase

  1. $map = Get-ADOrganizationalUnit -Filter * -Properties canonicalName |
  2. Group-Object canonicalName -AsHashTable -AsString
  3. $targetOUs | ForEach-Object {
  4. # 如果这个 `CanonicalName` 属于现有的组织单位
  5. if($map.ContainsKey($_)) {
  6. $queryParams = @{
  7. LDAPFilter = '(objectClass=user)'
  8. SearchBase = $map[$_].DistinguishedName
  9. SearchScope = 'OneLevel' # 仅查找直接的对象
  10. }
  11. foreach($object in Get-ADObject @queryParams) {
  12. [pscustomobject]@{
  13. Name = $object.Name
  14. Type = $object.ObjectClass
  15. SourceOU = $_
  16. }
  17. }
  18. }
  19. } | Export-CSV "Result.csv" -NTI

从技术上讲,计算机对象是用户类的一个子类,因此使用过滤器 (objectClass=user) 会在单个查询中找到计算机和用户。

英文:

Since you have a list of CanonicalNames then you would need to query all OUs to properly get the OU's DistinguishedName to use as -SearchBase:

  1. $map = Get-ADOrganizationalUnit -Filter * -Properties canonicalName |
  2. Group-Object canonicalName -AsHashTable -AsString
  3. $targetOUs | ForEach-Object {
  4. # if this `CanonicalName` belongs to an existing OU
  5. if($map.ContainsKey($_)) {
  6. $queryParams = @{
  7. LDAPFilter = '(objectClass=user)'
  8. SearchBase = $map[$_].DistinguishedName
  9. SearchScope = 'OneLevel' # looking only for immediate objects
  10. }
  11. foreach($object in Get-ADObject @queryParams) {
  12. [pscustomobject]@{
  13. Name = $object.Name
  14. Type = $object.ObjectClass
  15. SourceOU = $_
  16. }
  17. }
  18. }
  19. } | Export-CSV "Result.csv" -NTI

Technically, computer objects are a subclass of the user class hence using the filter (objectClass=user) would find both, computers and users in a single query.

huangapple
  • 本文由 发表于 2023年2月27日 11:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定