【发布时间】:2020-12-23 23:32:36
【问题描述】:
我对 Powershell 比较陌生,但一直无法在网上找到答案。
我正在尝试获取每个禁用用户的电子邮件数量以交换 2010,但还需要获取用户的标题表单 AD,因为组织使用 AD 中的 Title 属性按类型对用户进行分组
我已经编写了以下内容,但无法获得所需的数据,它只是将长度和数字返回到 CSV 文件,例如 “长度” “10” “3” “34”
如果我将 $title 排除在 $Disabled+= 的分配之外,则用户名和项目计数将添加到 csv 文件中,但我也确实需要标题。谁能指出我哪里出错了。
Import-Module ActiveDirectory
$i=0
$disUsers = Get-ADUser -Filter * -SearchBase "ou=User Disabled Accounts,dc=test,dc=com" -Properties SamAccountName,Title
$Disabled = @()
$disUsers | Foreach-Object{
$sam = $_.SamAccountName
$title = $_.Title
$mailDetail=Get-MailboxStatistics $sam | Select -Property DisplayName,ItemCount
$Disabled += $title, $mailDetail
$i++;
}
$Disabled | Export-Csv -Path $env:userprofile\desktop\DisabledADUserTitlewithMailbox.csv -NoTypeInformation
不幸的是,使用 Steve 提供的代码会出现以下错误
Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'ADCDisabledMail' Key being added: 'ADCDisabledMail'" ...
Exception calling "Add" with "2" argument(s): "Key cannot be null. Parameter name: key"...
编辑 在 Steven 的帮助下,我能够通过以下方式完成这项工作
'Import-Module ActiveDirectory'
$i=0
$disUsers=Get-ADUser -Filter {mailNickName -like '*'} -SearchBase "ou=User Disabled Accounts,dc=test,dc=com" -Properties SamAccountName,Title
$dis2 = $disUsers.count
$DisabledUser = @()
$disUsers | Foreach-Object{
Write-Host "Processing record $i of $dis2"
$sam = $_.SamAccountName
$title = $_.Title
$mailDetail=Get-MailboxStatistics $sam | Select-Object DisplayName, @{ Name = 'Title'; Expression = {$title}}, ItemCount
$DisabledUser+= $mailDetail
$i++;
}
$DisabledUser | Export-Csv -Path $env:userprofile\desktop\DisabledADUserTitlewithMailbox.csv -NoTypeInformation
【问题讨论】:
-
您的 CSV 仅包含
Length,因为您仅向Export-Csv发送字符串。Export-Csv查看管道中的第一个对象并将其属性作为 CSV 文件的标题。然后检索每个属性的值并为每个对象输出它们(每行一个对象)。一个字符串只有一个Length属性。所以你看到的结果是预期的。 -
感谢@AdminOfThings 的解释。
标签: arrays powershell active-directory exchange-server