###Send Exchange folder size
###Varabiles - which can me modified
$FromAddress = Mailserver
$ToAddress = '[email protected]
$MessageSubject = "Exchange Folder Size Report"
$MessageBody = "Attached is the current exchange folder size."
$SendingServer = "mail.server.com"
$filename = "C:\PowershellScripts\Directorysize\foldersize-$(get-date -f MMddHHssyyyy).txt"
Get-PSSnapIn -Reg | Add-PSSnapin -ea 0
###This gets the folder size from the designated section and puts it in a text file
function Get-Size($dir=".")
{
$ds = get-item $dir | % { $f = $_; get-childitem -r $_.FullName | measure-object -property length -sum -ErrorAction SilentlyContinue | select @{Name="Name";Expression={$f}},Sum}
$ds | foreach-object { if ( $_.Sum -le 999KB) {$_.Sum = ([string]::Format("{0:#.##}",($_.Sum)/1KB)) + " KB" ; $_ } elseif ( $_.Sum -le 999MB) {$_.Sum = ([string]::Format("{0:#.##}",($_.Sum)/1MB)) + " MB" ; $_ } elseif ( $_.Sum -le 999GB) {$_.Sum = ([string]::Format("{0:#.##}",($_.Sum)/1GB)) + " GB" ; $_ }}
} # End Get-Size
# set Alias of 'du' for Get-Size
Set-Alias du Get-Size
Get-Size e:\Exchange\* | out-file $filename
###Creates the mail message and adds the foldersize text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment($filename)
$SMTPMessage.Attachments.Add($Attachment)
###This sends the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
|