Powershell - 查询所有AD用户对象;返回电子邮件项目计数或没有邮箱

时间:2016-05-12 20:33:56

标签: 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 '*$*'} -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
}

所选用户的结果 enter image description here

针对所有用户的结果: enter image description here

2 个答案:

答案 0 :(得分:0)

填充$Mailboxes变量时,您缺少属性-Properties msExchRecipientTypeDetails。第5行和第6行应为If($mbx.msExchRecipientTypeDetails -eq $null)

更新:所以这不是完全正确的。如果您将If($aduser.msExchRecipientTypeDetails -eq $null)更改为-Properties EmailAddress,msExchRecipientTypeDetails,SamAccountName,则可以使用您的代码。但是,您真的不需要再次使用ADUser。 $ Mailboxes是ADUsers的集合。将第2,6,7行更改为$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 } } }

mydict.Add({value1},OriginalValue1);
mydict.Add({value2},OriginalValue2);

答案 1 :(得分:0)

@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

}