我已经启动了一个脚本,该脚本将AD中的用户名与CSV中的特定职位进行比较,然后列出用户所属的所有组(以及组内的组)。
我已经检查了第一个注释下的功能,该功能可以按预期工作,列出了用户的所有组。
脚本的下一部分无法按预期工作,当我尝试将用户列表的输出用作函数的变量时,我仅获得所有用户所属的组列表,而不是组和用户。
现在我得到以下结果:
组别1 组2 组3 组2 组3 组4
我的理想选择是:
用户成员 ---- -------- 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句