【问题标题】:Powershell Get-ADGroupMember does not return listPowershell Get-ADGroupMember 不返回列表
【发布时间】:2020-06-26 15:14:52
【问题描述】:

我正在尝试使用 PowerShell 来审核我们在 AD 中的所有安全组成员。我一直试图让Get-ADGroupMember 工作,但无论何时我尝试它,它都会返回消息'找不到具有身份'groupName'的对象:'DC = xxxx,DC = xxxx,DC = xxxx,DC = xxxx' .

我试过以下没有运气:

$groupNames = 'groupName1' , 'groupName2' , 'groupName3'
foreach ($group in $groupNames) {
   Get-AdGroupMember -Identity $group
}

有没有人成功地从 AD 编译安全组中的组成员列表并将其导出为 .CSV?

【问题讨论】:

  • -Identity 必须包含 GUIDobjectsiddistinguishednamesamaccountname。如果您正在尝试,例如displayname,除非它与samaccountname 匹配,否则它将无法工作。
  • 如果$groupnames包含name属性值,可以先查询可用属性再处理Get-AdGroupMember。例如,$groups = ($groupNames |% { Get-AdGroup -filter "Name -eq '$_'" }).SamAccountName.
  • @AdminOfThings 您的第一条评论是正确的。我使用的是“名称”而不是“samaccountname”,一旦替换了它,它就得到了我所需要的。是否可以运行 Get-ADGroupMember 并让它从文本文件中提取组名?另一件事。我能够运行 Get-ADGroup 函数,但它有很多我真的不需要的额外信息。我会使用 -filter 并指定 samaccountname 只是为了让它导出该名称吗?
  • 您的代码逻辑是从列表中读取的正确方法。要从文件file.txt 中获取SamAccountNames 的列表,您可以使用$groupNames = Get-Content file.txt
  • 出于某种原因,我仍然无法让“samaccountname”过滤器正常工作。我试过 get-adgroup -identity samaccountname get-adgroup -identity 'samaccountname' get-adgroup -filter 'samaccountname' 一直说找不到具有该身份的对象

标签: powershell active-directory-group


【解决方案1】:

使用Get-AdGroup* 命令查询 AD 组时需要考虑的事项很少。

  1. -Identity 参数只接受与对象的GuidObjectSidDistinguishedNameSamAccountName 匹配的值。如果您的输入不是这些属性值之一,您将需要运行另一个命令来检索正确的数据或更改您的列表。
  2. -Identity 只接受一个值,这意味着如果你想提供一个值列表,你需要遍历它们。
  3. Get-AdGroupMember 输出的属性/值对没有Get-AdUser 多。您不能强制它输出比它更多的属性。它没有像Get-AdUser 这样的-Properties 参数。有时需要同时使用这两个命令来获取所有需要的数据。
  4. 您可以使用Export-CsvGet-Ad* 输出发送到CSV。如果您不使用像 Select-Object 这样的任何属性过滤,则返回的属性名称将是 CSV 的列。属性的相关值将出现在行中,每一行代表一个返回的对象。您可以选择将命令的全部结果一次发送到 CSV,或者每次命令运行时使用Export-Csv -Append
  5. 使用Select-Object 仅输出您关心的属性。 Select-Object Property 输出一个自定义对象,其中仅包含属性 Property 和每个返回对象的 Property 值。如果只想返回值而不是自定义对象,可以使用Select-Object -Expand Property
  6. Get-Content 可用于读取文件。如果文件只包含一个值列表,可能是SamAccountName 值,您可以使用Get-Content file.txt 来检索该列表。该列表将是一个可以循环的数组。
  7. 由于Get-AdUser 可能很冗长,因此明智的做法是使用-Properties 参数显式列出您要返回的默认设置之外的任何额外属性。 -Properties * 将返回所有属性,但这不是最佳做法。

鉴于以上考虑,我会做以下事情:

$groupNames = 'groupName1' , 'groupName2' , 'groupName3'

# Alternatively, if you have a file (file.txt) with your group names listed as one group per line
$groupNames = Get-Content file.txt

# The Foreach-Object section is only needed if $groupNames does not contain a valid -Identity value
# The Filter below uses Name attribute as an example because it assumes $groupNames contains Name attribute values. If it contains another attribute, update the filter accordingly.
$SamAccountNames = $groupNames | Foreach-Object {
    Get-AdGroup -Filter "Name -eq '$_'" | Select-Object -Expand SamAccountName
}

# Storing the loop output into a variable is efficient provided you have enough memory for the operation.
# Alternatively, you can just pipe the `Get-AdGroupMember` into `Export-Csv -Append` but that could be a lot of writes!
$output = foreach ($group in $SamAccountNames) {
   Get-AdGroupMember -Identity $group # You can use Select-Object here for specific properties
}
$output | Export-Csv -Path output.csv -NoTypeInformation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多