【发布时间】:2018-06-15 22:46:20
【问题描述】:
列出所有拥有邮箱但不在名为 Metalogix* 的组中的用户。我需要一个 PowerShell 脚本来检查特定用户是否属于某个组,以及该用户是否属于这些组中的任何一个。
我已经有了工作脚本:
Import-Module ActiveDirectory
$Users = Get-Mailbox -ResultSize "unlimited"
$Group = "Metalogix*"
foreach ($user in $Users) {
$Check = Get-ADPrincipalGroupMembership -Identity $User.sAMAccountName |
? { $_.Name -like $Group }
if ($Check -eq $null) {
Write-Output "$User.sAMAccountName is NOT part of this group"
} else {
$Results = Get-Mailbox -Identity $User.sAMAccountName |
select Name, sAMAccountName, PrimarySmtpAddress, Database |
Export-csv "c:\results1.csv" -NTI -Append
}
}
但脚本没有递归列出组,例如 tester4-6 是“测试组 2”的成员,它是“测试组 1”的成员。其余的都是直接的。只是我可以看到直接成员资格,而不是递归成员资格。
第二个问题:我想获取所有 samaccountname 以“STR”前缀开头的用户。
测试组 1 测试仪1 测试仪2 -> 测试组 2 测试仪4 测试仪6【问题讨论】:
-
我的 Github 上有几个脚本可以对 AD 组成员进行递归搜索,您可以根据需要进行修改。 github.com/trebleCode/theposhadmin 我的 Get-NestedMembers 和 Get-UserMemberships 脚本可能会有所帮助。他们提示选择文件,但您应该能够根据需要进行修改。另请查看此处有关类似问题的另一篇文章:stackoverflow.com/questions/23885149/… 对于第二部分:
Get-ADUser -Filter {samaccountname -like "STR*"}Use Group-Object -
如果您要报告的内容,请选择按主体名称分组
标签: powershell active-directory