PowerShell 脚本用于显示 AD 中 OU 内 AD 安全组的所有用户部分

发布于 2025-01-12 16:11:52 字数 655 浏览 0 评论 0原文

我是 PowerShell 新手,我正在尝试显示某个 OU 内的所有 AD 安全组,并显示该安全组的每个用户部分。

我想将用户名和名称以及安全组名称显示到 CSV 文件中。

我已设法获取此信息,但我必须在脚本本身中手动添加 AD 安全组名称:

$groups = "GroupName1", "GroupName2", "GroupName3", "GroupName4", "GroupName5"

$results = foreach ($group in $groups) {
    Get-ADGroupMember $group | select samaccountname, name, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
}

$results

$results | Export-csv C:\Users\Sam\Desktop\Users.csv -NoTypeInformation 

上面的脚本输出我需要的信息,但如上所述,我必须在脚本本身中手动输入安全组名称。

我认为我需要使用的命令是 Get-ADGroup

任何帮助,谢谢。

I am new to PowerShell I am trying to display all AD security groups within a certain OU and to display each user part of that security group.

I want to show username and name and the security group name to a CSV file.

I have managed to get this information but I had to manually add the AD security group name within the script itself:

$groups = "GroupName1", "GroupName2", "GroupName3", "GroupName4", "GroupName5"

$results = foreach ($group in $groups) {
    Get-ADGroupMember $group | select samaccountname, name, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
}

$results

$results | Export-csv C:\Users\Sam\Desktop\Users.csv -NoTypeInformation 

The above script outputs the information I require but as stated above I have to manually enter the Security GroupName within the script itself.

I think the command I need to use is Get-ADGroup

Any help is appreciated thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2025-01-19 16:11:52

您可以使用 Get-ADGroup -Filter * -SearchBase 'OUdnHere' 搜索所需组织单位下的所有群组,然后只需应用已有的相同逻辑即可:

  1. 循环遍历群组
  2. 获取其 导出
  3. 构建输出
  4. 为 CSV
$ou = 'distinguished name of my OU here'
Get-ADGroup -Filter * -SearchBase $ou -Properties Description | ForEach-Object {
    foreach($member in Get-ADGroupMember $_) {
        [pscustomobject]@{
            SamAccountName = $member.SamAccountName
            Name           = $member.Name
            GroupName      = $_.Name
            Description    = $_.Description
        }
    }
} | Export-csv C:\Users\Sam\Desktop\Users.csv -NoTypeInformation 

You can use Get-ADGroup -Filter * -SearchBase 'OUdnHere' to search for all groups under your desired Organizational Unit, then you can simply apply the same logic you already have:

  1. Loop over the Groups
  2. Get their memberships
  3. Construct the output
  4. Export to CSV
$ou = 'distinguished name of my OU here'
Get-ADGroup -Filter * -SearchBase $ou -Properties Description | ForEach-Object {
    foreach($member in Get-ADGroupMember $_) {
        [pscustomobject]@{
            SamAccountName = $member.SamAccountName
            Name           = $member.Name
            GroupName      = $_.Name
            Description    = $_.Description
        }
    }
} | Export-csv C:\Users\Sam\Desktop\Users.csv -NoTypeInformation 
何其悲哀 2025-01-19 16:11:52

尝试这样的事情(当然为 OU 添加正确的 dn):

$Groups = Get-AdGroup -SearchBase "ou=OuName,DC=contoso,DC=local" -Filter * | Select -ExpandProperty Name

或者根据我的说法,稍微短一些并且更容易阅读:

$Groups = (Get-AdGroup -SearchBase "ou=OuName,DC=contoso,DC=local" -Filter *).Name

未经测试的代码,但您已经了解了它的要点。

Try something like this (add the correct dn for the OU of course):

$Groups = Get-AdGroup -SearchBase "ou=OuName,DC=contoso,DC=local" -Filter * | Select -ExpandProperty Name

Or somewhat shorter and easier to read according to me:

$Groups = (Get-AdGroup -SearchBase "ou=OuName,DC=contoso,DC=local" -Filter *).Name

Untested code, but you get the gist of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文