PowerShell - Remote Mailbox Monitoring

Remote Mailbox Monitoring



# Load Snap-In
html
exchange-ews
# Variable
    $SendNotification = $False
    $From = 'powershell@ACME.com'
   
    [int]$hour = get-date -format HH
    [string]$DayofWeek = (get-date).dayofweek
    # write-host $DayofWeek

    $DayTimes = @{
"Monday"="7;21"
"Tuesday"="7;21"
"Wednesday"="7;21"
"Thursday"="7;21"
"Friday"="7;21"
"Saturday"="9;17"
"Sunday"="9;17"
}
    $DayTimeInfo = $DayTimes[$DayofWeek].split(";")
    $EndTime = $DayTimeInfo[1]
    $Starttime = $DayTimeInfo[0]
    write-host $Hour $EndTime $Starttime

# Mailbox
$mailboxname = "Support@ACME.com"
# Open Exchange Mailbox
$service = new-object microsoft.exchange.webservices.data.exchangeservice([microsoft.exchange.webservices.data.exchangeversion]::exchange2010_SP2)
$service.autodiscoverurl($mailboxname)
$folderid = new-object microsoft.exchange.webservices.data.folderid([microsoft.exchange.webservices.data.wellknownfoldername]::inbox,$mailboxname)
$folderview = new-object microsoft.exchange.webservices.data.folderview(1000)
# Query Inbox
$inbox = [microsoft.exchange.webservices.data.folder]::bind($service,$folderid)
$inbox.UnreadCount

if (($inbox.UnreadCount -gt 0) -and (($hour -le $StartTime) -or ($hour -ge $EndTime)))  {
    $SendNotification = $true
}
write-host $SendNotification
if ($SendNotification) {
        $to = "help@NeedHelp.com"
        $cc = "BBunny@ACME.com"
$subject = "Acme CO. - Unread Messages"
$body = "Please be advised that there is a new email in the Acme CO. Support Mailbox

"
$body += "
* Please do not reply to this email as the powershell mailbox is not monitored. *

"
        #$smtpserver = "SMTP MAIL SERVER HERE"
send-mailmessage -to $to -cc $cc -from $from -subject $subject -bodyashtml $body -smtpserver $smtpserver

}