2008-09-10

Exchange 2007 - Detailed Mailbox Statistics (PowerShell)

Toz jsem dal do kupy skript na generovani sumarnejsich mailbox statistik. Treba se to bude hodit i nekomu jinymu :)

## setup

$bgc1 = "Black"
$fgc1 = "White"
$bgc2 = "DarkCyan"
$fgc2 = "Red"
$daysback = 14

##functions

function ralign([string]$str, [int]$chars){
  $result = ""
  for ($i = 0; $i -lt $chars-$str.Length; $i++) {$result += " "}
  $result += $str
  $result
}


## MAIN

Write-Progress -Id 1 -Activity "Enumerating mailboxes..." -Status "Initializing..." -PercentComplete 0

Write-Host -ForegroundColor yellow -backgroundcolor darkcyan "`nMailbox Statistics: `n"

$a = $null
$mbx = $null
$mbx = @{}
[int]$mbxCount = (Get-MailboxServer | Get-Mailbox | Measure-Object).Count
[int]$mbxOldCount = 0
[int]$mbxBigCount = 0
$i = 1
Get-MailboxServer | Get-Mailbox | Sort Name | foreach {
  [string]$status = [string]([math]::round(($i/$mbxCount)*100))
  $status += "%"
  Write-Progress -Id 1 -Activity "Enumerating mailboxes..." -CurrentOperation $_.Name -Status $status -PercentComplete ([math]::round(($i/$mbxCount)*100))
  $i+=1
  $eg = [string]$_.ExchangeGuid
  [array]$a += ($eg)
  [string]$iwq = [string]$_.IssueWarningQuota
  If ($iwq -eq "unlimited") {$iwq = "0MB"}
  If ($iwq.substring($iwq.Length-2,2) -eq "KB")
    {[int]$iwq = [math]::round([string]($iwq.substring(0,$iwq.Length-2)) / 1024, 0)}
  Else
    {[int]$iwq = ([string]$iwq.substring(0,$iwq.Length-2))}

  [string]$psq = [string]$_.ProhibitSendQuota
  If ($psq -eq "unlimited") {$psq = "0MB"}
  If ($psq.substring($psq.Length-2,2) -eq "KB")
    {[int]$psq = [math]::round([string]($psq.substring(0,$psq.Length-2)) / 1024, 0)}
  Else
    {[int]$psq = ([string]$psq.substring(0,$psq.Length-2))}

  [string]$prq = [string]$_.ProhibitSendReceiveQuota;
  If ($prq -eq "unlimited") {$prq = "0MB"}
  If ($prq.substring($prq.Length-2,2) -eq "KB")
    {[int]$prq = [math]::round([string]($prq.substring(0,$prq.Length-2)) / 1024, 0)}
  Else
    {[int]$prq = ([string]$prq.substring(0,$prq.Length-2))}

  $mbx[$eg] = @{Name = $_.Name; IssueWarningQuota = $iwq; ProhibitSendQuota = $psq; ProhibitSendReceiveQuota = $prq}
  Get-MailboxStatistics -Identity $eg | foreach {
    [string]$tis = [string]$_.TotalItemSize
    If ($tis.substring($tis.Length-2,2) -eq "KB")
      {[int]$tis = [math]::round([string]($tis.substring(0,$tis.Length-2)) / 1024, 0)}
    Else
      {[int]$tis = [math]::round([string]($tis.substring(0,$tis.Length-1)) / 1024 / 1024, 0)}
    
    [string]$tds = [string]$_.TotalDeletedItemSize
    If ($tds.substring($tds.Length-2,2) -eq "KB")
      {[int]$tds = [math]::round([string]($tds.substring(0,$tds.Length-2)) / 1024, 0)}
    Else
      {[int]$tds = [math]::round([string]($tds.substring(0,$tds.Length-1)) / 1024 / 1024, 0)}
    $mbx[$eg] += @{TotalItemSize = $tis; TotalDeletedItemSize = $tds; StorageLimitStatus = $_.StorageLimitStatus; LastLogonTime = $_.LastLogonTime}
  }
}
Write-Progress -Id 1 -Activity "Enumerating mailboxes..." -Status "100%" -PercentComplete 100 -Completed

Write-Host -ForeGroundColor White "Name `t`t`tItem `tDelItem Warning`tProhib`tProhib`tLastLogon"
Write-Host -ForeGroundColor White "`t`t`t`Size`tSize`tQuota`tSend`tReceive"
Write-Host -ForeGroundColor White "-----------------------`t-------`t-------`t-------`t-------`t-------`t---------"

foreach ($eg in $a) {
  $bgc = $bgc1
  $fgc = $fgc1
  If ($mbx[$eg].LastLogonTime -lt (Get-Date).AddDays(-($daysback)))
    {$bgc = $bgc2; $mbxOldCount++}
  If ($mbx[$eg].StorageLimitStatus -ne "BelowLimit")
    {$fgc = $fgc2; $mbxBigCount++}
  If ($mbx[$eg].Name.Length -gt 14)
    {Write-Host -NoNewLine -ForegroundColor $fgc -BackgroundColor $bgc $mbx[$eg].Name "`t"}
  Else
    {Write-Host -NoNewLine -ForegroundColor $fgc -BackgroundColor $bgc $mbx[$eg].Name "`t`t"}
  If ($mbx[$eg].StorageLimitStatus -eq "BelowLimit")
    {Write-Host -NoNewLine -BackgroundColor $bgc (ralign $mbx[$eg].TotalItemSize 4) "M`t"}
  Else
    {Write-Host -NoNewLine -ForegroundColor Red -BackgroundColor $bgc (ralign $mbx[$eg].TotalItemSize 4) "M`t"}
  
  Write-Host -NoNewLine -BackgroundColor $bgc (ralign $mbx[$eg].TotalDeletedItemSize 4) "M`t" (ralign $mbx[$eg].IssueWarningQuota 4) "M`t" (ralign $mbx[$eg].ProhibitSendQuota 4) "M`t" (ralign $mbx[$eg].ProhibitSendReceiveQuota 4) "M`t"
  Write-Host -BackgroundColor $bgc (Get-Date $mbx[$eg].LastLogonTime -Format "yy-MM-dd hh:mm")
}
Write-Host "`nSummary:`n"
Write-Host "Total number of mailboxes: `t$mbxCount"
Write-Host "Mailboxes over quota limit: `t" -NoNewLine; Write-Host -ForegroundColor $fgc2 $mbxBigCount
Write-Host "Mailboxes not used for $daysback days: " -NoNewLine; Write-Host -BackgroundColor $bgc2 $mbxOldCount
Write-Host

$mbxCount = $null
$mbxBigCount = $null
$mbxOldCount = $null

$bgc = $null
$fgc = $null
$bgc1 = $null
$fgc1 = $null
$bgc2 = $null
$fgc2 = $null

$tis = $null
$tds = $null
$iwq = $null
$psq = $null
$prq = $null

$a = $null
$mbx = $null
$eg = $null
$i = $null

1 komentář:

  1. Ted jsem......nasel neco podobnyho, co je mozna pro davkovy spracovani lepsi - exportuje to CSV: http://smtpport25.wordpress.com.../

    OdpovědětVymazat

Mazat komentáře nehodlám, výjimky však tvoří vulgární a off-topic komentáře!