![](/img/trans.png)
[英]Powershell script to remove members from AD groups not working on all groups/users
[英]Script to list users and all the groups they are members of
我已經啟動了一個腳本,該腳本將AD中的用戶名與CSV中的特定職位進行比較,然后列出用戶所屬的所有組(以及組內的組)。
我已經檢查了第一個注釋下的功能,該功能可以按預期工作,列出了用戶的所有組。
腳本的下一部分無法按預期工作,當我嘗試將用戶列表的輸出用作函數的變量時,我僅獲得所有用戶所屬的組列表,而不是組和用戶。
現在我得到以下結果:
Group1 Group2 Group3 Group2 Group3 Group4
我的理想選擇是:
User MemberOf ---- -------- Bob Group1, Group2, Group3.... Jim Group2, Group3, Group4....
碼:
#Function to recursively check account for group membership
function Get-SubGroups ($account)
{
$ErrorActionPreference = "SilentlyContinue"
$groups = Get-ADPrincipalGroupMembership $account
$allGroups =$null
while ($groups)
{
$allGroups += $groups.Name
$groups = $groups.Name | Get-ADPrincipalGroupMembership
}
$allGroups
}
#CSV with list of job titles and command to get the first user with a job title
$titles = Get-Content 'B:\JobTitles.csv'
#Command to get the list of groups for each user
$users = foreach ($title in $titles)
{
Get-ADUser -Filter 'Title -Like $title' -Properties Name, SamAccountName, MemberOf, Title | select -First 1
}
foreach ($user in $users)
{
Get-SubGroups $user | Write-Output
}
你的意思是這樣的:
#Function to recursively check account for group membership
function Get-SubGroups ($account)
{
$ErrorActionPreference = "SilentlyContinue"
$groups = Get-ADPrincipalGroupMembership $account
$allGroups =$null
While($groups)
{
$allGroups += $groups.Name
$groups = $groups.Name | Get-ADPrincipalGroupMembership
}
$allGroups
}
#CSV with list of job titles and command to get the first user with a job title
$titles = Get-Content 'B:\JobTitles.csv'
$users = @()
foreach ($title in $titles)
{
$users += Get-ADUser -Filter 'Title -Like $title' -Properties Name, SamAccountName, MemberOf, Title | select -First 1
}
$users | Format-Table -Wrap @{Expression={$_.Name};Label="User";width=25},
@{Expression={[string[]](Get-SubGroups $_) -join ' '};Label="MemberOf"} | Write-Output
輸出將是:
User MemberOf
---- --------
SomeUser Group1 Group2 Group3
SomeUser2 Group2 Group3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.