【发布时间】:2020-11-25 21:19:58
【问题描述】:
我是从头开始写的,所以如果有更好的方法,感谢任何建议。
我正在尝试列出来自多个 AD 组的所有用户,方法是使用通配符作为 AD 组名称,并包括每个用户的信息,包括他们的经理。 这行得通,但我不知道如何更好地格式化经理字段;它作为完整的 DN 返回。 我试过了:
- 将 $mgr 格式化为字符串(.length 等,但这不起作用,似乎也无法将其转换为字符串)
- 格式化为数组(只有 1 个值)
- 我认为这是一个对象,但无法格式化
- 格式为表达式(我的第一次尝试) - 它有点工作,但在每一行中显示字段名称
如何更好地格式化经理字段,或返回经理姓名以开始? 最终数据实际上被导出为 CSV,因此格式正确。
谢谢, 码头
Clear-Host
$FilterToUse = "grp-Servers-Win-"
$FilterToUse2 = $FilterToUse + "*"
$Groups = Get-ADGroup -filter {name -like $FilterToUse2}
$headers = "GroupName,Network ID,Name,UserType,Check"
Write-Host $headers
ForEach ($Group in $Groups) {
Get-ADGroupMember $group | ForEach-Object {
$mgr = Get-ADUser $_.samaccountname -Properties manager | Select -Property manager
#$mgr = Get-ADUser $_.samaccountname -Properties manager | Select @{label='ad-manager';expression={$_.manager -replace '^CN=|,.*$'}}
if ($_.samaccountname -like "ZZZ*") {
Write-host ($Group.Name + "," + $_.samaccountname + "," + $_.name + "," + $_.objectclass + ",CatA," + $mgr)
}
elseif ($_.samaccountname -like "YYY*") {
Write-Host ($Group.Name + "," + $_.samaccountname + "," + $_.name + "," + $_.objectclass + ",CatB," + $mgr)
}
elseif ($_.samaccountname -like "XXX*") {
Write-Host ($Group.Name + "," + $_.samaccountname + "," + $_.name + "," + $_.objectclass + ",CatC," + $mgr)
}
else {
Write-Host ($Group.Name + "," + $_.samaccountname + "," + $_.name + "," + $_.objectclass + ",check," + $mgr)
}
}
}
【问题讨论】:
-
如果您希望管理器作为 ADUser 对象,则需要执行额外的查询:
Get-ADUser $_.samaccountname -Properties manager | Select -ExpandProperty manager | get-aduser。如果要操作 DN 字符串,则需要返回属性值而不是带有属性的自定义对象:Get-ADUser $_.samaccountname -Properties manager | Select -ExpandProperty manager。注意-ExpandProperty正在使用中。 -
@AdminOfThings 是对的,如果您只想检索经理的姓名,这里是整行:
$mgr = Get-ADUser $_.samaccountname -Properties manager | Select -ExpandProperty manager | Get-ADUser | Select -ExpandProperty Name。你应该避免过滤器的大括号,所以更喜欢这个$Groups = Get-ADGroup -filter "name -like '$FilterToUse2'" -
太棒了,感谢大家的解释和快速回答
标签: powershell active-directory