我已经在我们的广告中列出了用户:
Get-ADUser -Filter * -Properties department |
Where-Object {$_.department -Like "F0*"} |
Select sAMAccountName, department
它会输出所有感兴趣的用户。
现在,我想遍历这些用户,并找到所有这些用户都是一个或几个Exchange通讯组的管理员。并有一个包含用户名和通讯组名称的输出,这可能吗?
答案 0 :(得分:1)
正如Paxz已经评论过的那样,您应该对此采取“另一种方式”。如果要获取通讯组经理的姓名和部门,请先获取姓名和部门。
类似的事情可能会做到:
# get the distribution groups where the ManagedBy property is set
Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
# then go through all possible listed managers and get their DisplayName and Department
foreach ($id in $_.ManagedBy) {
try {
# use '-ErrorAction Stop' to make sure the catch block is entered upon failure
$manager = Get-AdUser -Identity $id -Properties DisplayName, Department -ErrorAction Stop
$mgrName = $manager.DisplayName
$mgrDept = $manager.Department
}
catch {
# output failed result(s)
$mgrName = 'Obsolete user'
$mgrDept = 'Unknown'
}
# output the result(s) as PSObjects
New-Object -TypeName PSObject -Property @{
'Distribution Group' = $_.Name
'Manager' = $mgrName
'Department' = $mgrDept
}
}
}
如果要将结果存储在csv文件中,可以将脚本扩展为以下内容:
$fileName = '<Enter the full path and filename here for the output csv file>'
# collect all results in an array
$results = @()
Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
foreach ($id in $_.ManagedBy) {
try {
# use '-ErrorAction Stop' to make sure the catch block is entered upon failure
$manager = Get-AdUser -Identity $id -Properties DisplayName, Department -ErrorAction Stop
$mgrName = $manager.DisplayName
$mgrDept = $manager.Department
}
catch {
# output failed result(s)
$mgrName = 'Obsolete user'
$mgrDept = 'Unknown'
}
$results += New-Object -TypeName PSObject -Property @{
'Distribution Group' = $_.Name
'Manager' = $mgrName
'Department' = $mgrDept
}
}
$results | Export-Csv -Path $fileName -UseCulture -NoTypeInformation -Force