Blog/Exchange/mailbox_size_multidomain.ps1

57 lines
2.3 KiB
PowerShell
Raw Permalink Normal View History

2023-12-16 16:37:57 +01:00
#Script créé par Nicolas Lang - Sous licence CC-BY-SA
2023-12-16 16:33:33 +01:00
#https://nicolaslang.fr
2023-12-16 12:25:06 +01:00
Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010
$domaines = get-accepteddomain
$nomduserveur = "Entrez le nom de votre serveur"
$expediteur = "Adresse expediteur du rapport par mail"
$destinataire = "Adresse destinataire du rapport par mail"
$smtpserver = "Entrez le nom du serveur smtp qui enverra le mail"
$boites = Get-Mailbox -Server $nomduserveur
$tableau = @()
$chemin = [Environment]::GetFolderPath("Desktop")
$code = [System.Text.Encoding]::Utf8
foreach ($boite in $boites)
{
$statsboite = Get-MailboxStatistics $boite
$statsboite |ft
$ajout = New-Object PSObject
#$domaineboite = get-mailbox $statsboite.displayname
$domaineboite = ($boite.primarysmtpaddress).domain
Clear-Variable taille -ErrorAction silentlycontinue
$taille = $statsboite.TotalItemSize.Value.ToMB()
if ($taille -ne $null)
{
$ajout | Add-Member -MemberType Noteproperty -Name "Nom" -Value $statsboite.displayname
$ajout | Add-Member -MemberType Noteproperty -Name "Domaine" -Value $domaineboite
$ajout | Add-Member -MemberType Noteproperty -Name "Taille (en Mb)" -Value $taille
$tableau += $ajout
}
}
set-location c:\
$fichiers = @()
Foreach ($domaine in $domaines)
{
$tableauclient = $tableau | Where-Object {$_.domaine -eq $domaine.domainname}
$tableauclient = $tableauclient |Sort-Object -Property "Taille (en Mb)" -Descending
if ($tableauclient -ne $null)
{
$filename = ($domaine.name) +" " +(get-date).toshortdatestring().replace("/","_")
$repertoire = ($chemin+"\Export\"+($domaine.name))
$fichier = ($repertoire+"\"+$filename)+".csv"
if ((Test-Path $repertoire) -eq $false){ New-Item -ItemType directory -Path $repertoire}
$tableauclient | Export-csv -encoding UTF8 -Path $fichier -NoTypeInformation -Force -Delimiter ";"
$date = get-date
$body = "Export des données le " + $date
$fichiers += $fichier
}
}
$fichiers = $fichiers |sort-object
Send-MailMessage -Subject "Export $date" -Attachments $fichiers -Body $body -Encoding $code -From $expediteur -To $destinataire -SmtpServer $smtpserver