29 January 2014

Get active OWA sessions by email

Sometimes you have to know how many users are connected to your Outlook Web App.

I brew up some script to show you just that:

# Script: Get-OWASessions.ps1
# Purpose: Shows the users currently connected to OWA
# Author: Edwin van Brenk
# Date: 28-01-2014
# For:         Company

#Functions
$filename = "D:\Temp\OWASessions.html"
$date = get-date -Format dd-MM-yyyy 
$smtpServer = "smtp.company.lan"
$smtpFrom = "OWASessions@company.com"
$smtpTo = "username@company.com"
$messageSubject = "Active OWA Sessions $date"

Write-Host "Deleting temp file"
#If outputfile already exsists then delete
if (Test-Path($filename))
{
remove-item $filename
}

write-host "Getting OWA sessions please wait"
$htmlreport = Get-Mailbox -resultsize unlimited | Get-LogonStatistics | Where-Object {$_.ApplicationID -eq "Client=OWA"} | Select-Object UserName,LastAccessTime | ConvertTo-Html
Write-host "Creating output file"
$htmlreport | Out-File $filename
write-host "Getting ready to send the email"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$message.Body = ( $htmlreport | out-string )
$message.Body = $htmlreport
write-host "Sending email"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

Write-Host "Done"

No comments:

Post a Comment