PowerShell 脚本用于显示 AD 中 OU 内 AD 安全组的所有用户部分
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Get-ADGroup -Filter * -SearchBase 'OUdnHere'
搜索所需组织单位下的所有群组,然后只需应用已有的相同逻辑即可: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:尝试这样的事情(当然为 OU 添加正确的 dn):
或者根据我的说法,稍微短一些并且更容易阅读:
未经测试的代码,但您已经了解了它的要点。
Try something like this (add the correct dn for the OU of course):
Or somewhat shorter and easier to read according to me:
Untested code, but you get the gist of it.