Powershell-查询所有AD用户对象;返回电子邮件项目计数或没有邮箱
当输入几个用户时,下面的代码失败,但在AD中查询所有用户时,并没有返回预期结果。不理解为什么批量用户和相对较小的用户列表失败 代码如下:Powershell-查询所有AD用户对象;返回电子邮件项目计数或没有邮箱,powershell,exchange-server,Powershell,Exchange Server,当输入几个用户时,下面的代码失败,但在AD中查询所有用户时,并没有返回预期结果。不理解为什么批量用户和相对较小的用户列表失败 代码如下: $Users = @('user1',"user2",'user3','user4') $Mailboxes = $Users | Get-ADuser -pr * $OU = 'DC=local,DC=local,DC=org' $Mailboxes = Get-ADUser -Filter {SamAccountName -notlike '*$*'}
$Users = @('user1',"user2",'user3','user4')
$Mailboxes = $Users | Get-ADuser -pr *
$OU = 'DC=local,DC=local,DC=org'
$Mailboxes = Get-ADUser -Filter {SamAccountName -notlike '*$*'} -pr samaccountname
$Mailboxes = Get-ADUser -Filter * -SearchBase $OU -Properties samaccountname
foreach ($Mbx in $Mailboxes)
{
$ADUser = Get-ADUser $Mbx.SamAccountName -Properties * #Enabled,AccountExpirationDate
$UserObj = New-Object PSObject
$UserObj | Add-Member NoteProperty -Name "Username" -Value $ADUser.SamAccountName
If($mbx.msExchRecipientTypeDetails -eq $null)
{
$UserObj | Add-Member NoteProperty -Name "E-Mail" -Value "NoEmailAddress"
$UserObj | Add-Member NoteProperty -Name "email ItemCount" -Value "NoMailBox"
$UserObj | Add-Member NoteProperty -Name "TotalItemSize(GB)" -Value "NoMailBox"
Write-Host $Mbx.SamAccountName "has no mailbox" -ForegroundColor green
}
Else
{
If($mbx.msExchRecipientTypeDetails -eq 1)
{
$stats = $Mbx.EmailAddress | Get-MailboxStatistics | Select-Object TotalItemSize,TotalDeletedItemSize,ItemCount,LastLogonTime,LastLoggedOnUserAccount
$MbxSizeb = $stats.TotalItemSize -replace "(.*\()|,| [a-z]*\)"
$Tmp_gb = $MbxSizeb/1GB
$MbxSizeGB = [math]::Round($Tmp_gb,2)
$UserObj | Add-Member NoteProperty -Name "E-Mail" -Value $ADUser.EmailAddress
$UserObj | Add-Member NoteProperty -Name "email ItemCount" -Value $stats.ItemCount
$UserObj | Add-Member NoteProperty -Name "TotalItemSize(GB)" -Value $MbxSizeGB
}
}
$Report = $Report += $UserObj
}
选定用户的结果
针对所有用户的结果:
填充
$Mailboxes
变量时,缺少属性msExchRecipientTypeDetails
。第5行和第6行应为-Properties msExchRecipientTypeDetails
更新:所以这不是完全正确的。如果将
if($mbx.msExchRecipientTypeDetails-eq$null)
更改为if($aduser.msExchRecipientTypeDetails-eq$null)
,则可以使用代码。然而,你真的不需要再次得到阿杜$邮箱是Aduser的集合。将第2、6、7行更改为-属性EmailAddress、msExchRecipientTypeDetails、SamAccountName
$Users = @('user1',"user2",'user3','user4')
$Mailboxes = $Users | Get-ADuser -Properties EmailAddress,msExchRecipientTypeDetails,SamAccountName
#$OU = 'DC=local,DC=local,DC=org'
#$Mailboxes = Get-ADUser -Filter {SamAccountName -notlike '*$*'} -Properties EmailAddress,msExchRecipientTypeDetails,SamAccountName
#$Mailboxes = Get-ADUser -Filter * -SearchBase $OU -Properties EmailAddress,msExchRecipientTypeDetails,SamAccountName
$Report = @()
foreach ( $Mbx in $Mailboxes ) {
switch ( $Mbx.msExchRecipientTypeDetails ) {
1 {
$Stats = $Mbx.EmailAddress | Get-MailboxStatistics | Select-Object TotalItemSize,ItemCount
$Report += [pscustomobject] @{
'Username' = $_.SamAccountName
'E-Mail' = $Mbx.EmailAddress
'E-Mail ItemCount' = $Stats.ItemCount
'TotalItemSize(GB)' = ( [math]::Round( ( $Stats.TotalItemSize -replace "(.*\()|,| [a-z]*\)" )/1GB ),2 )
}
break
}
default {
$Report += [pscustomobject] @{
'Username' = $_.SamAccountName
'E-Mail' = 'NoEmailAddress'
'E-Mail ItemCount' = 'NoMailBox'
'TotalItemSize(GB)' = 'NoMailBox'
}
break
}
}
}
@Shawn Esterman-感谢您的输入--下面的代码非常有用。显式声明要搜索的属性
cls
$Users = @()
$Report = @()
$UserObj = @()
$ADUser = @()
$Mbx = @()
$OU = @()
$Mailboxes = @()
$Users = @("user1",'user2','user3','user4','user5')
$Mailboxes = $Users | Get-ADuser -Properties EmailAddress,msExchRecipientTypeDetails,SamAccountName
$OU = 'DC=local,DC=local,DC=local'
$Mailboxes = Get-ADUser -SearchBase $OU -Filter {SamAccountName -notlike '*$*'} -ResultSetSize 50 -Properties EmailAddress,msExchRecipientTypeDetails,SamAccountName
foreach ($Mbx in $Mailboxes)
{
$UserObj = New-Object PSObject
$UserObj | Add-Member NoteProperty -Name "Username" -Value $Mbx.SamAccountName
If($mbx.msExchRecipientTypeDetails -eq $null)
{
$UserObj | Add-Member NoteProperty -Name "E-Mail" -Value "NoEmailAddress"
$UserObj | Add-Member NoteProperty -Name "email ItemCount" -Value "NoMailBox"
$UserObj | Add-Member NoteProperty -Name "TotalItemSize(GB)" -Value "NoMailBox"
Write-Host $Mbx.SamAccountName "has no mailbox" -ForegroundColor green
}
Else
{
If($mbx.msExchRecipientTypeDetails -eq 1)
{
$stats = $Mbx.EmailAddress | Get-MailboxStatistics | Select-Object TotalItemSize,TotalDeletedItemSize,ItemCount,LastLogonTime,LastLoggedOnUserAccount
$MbxSizeb = $stats.TotalItemSize -replace "(.*\()|,| [a-z]*\)"
$Tmp_gb = $MbxSizeb/1GB
$MbxSizeGB = [math]::Round($Tmp_gb,2)
$UserObj | Add-Member NoteProperty -Name "E-Mail" -Value $Mbx.EmailAddress
$UserObj | Add-Member NoteProperty -Name "email ItemCount" -Value $stats.ItemCount
$UserObj | Add-Member NoteProperty -Name "TotalItemSize(GB)" -Value $MbxSizeGB
}
}
$Report = $Report += $UserObj
}请不要使用
-Prop*
,除非这是一个中小型域名。我只是在一个域上运行了一个脚本,花了一天时间才完成,我只是在寻找任何受计算机对象影响的组,结果它消耗了超过12 Gig的内存。“它可以加起来很快。”这位技术员说,你不可能在那里的某个地方阻止它,这似乎很愚蠢。通过不使用*和-Properties EmailAddress、msExchRecipientTypeDetails、SamAccountNameI可以很容易地解决这个问题。我没有使用-Prop*
,我只获取默认属性,实际上只存储组中每个对象的ObjectClass。诚然,我与将近700000名用户和计算机一起工作,但我的观点仍然是,如果他们查询整个active directory,他们可能不想在查询中使用-Prop*
。