Help with a FOREACH command - Exchange CMDLETS
good morning everyone!!
i have need monitor group of mailboxes older messages...i found script. obviously new powershell, want able check multiple mailboxes (script single mailboxes) without running 25different times. so remembered foreach command. so add script list of mailbox names script??
would get-content? i not sure here...like said...i newbie...
i appreciate help!
kevin
#######################################################################
# author : tom anderson - http://tomandersonpro.net
# date : 18-03-2013
# comment : this script checks the oldest item receive date for mails
# : in the specified mailbox. if a mail older than 10 minutes
# : is present an e-mail notification is sent.
# history :
#######################################################################
#### set current date and correct format
$date = (get-date).tostring('g')
### set mailbox identity
$mailbox = "yourmailbox"
#### e-mail variables
$smtpserver = "smtp.yourcompany.com"
$recipient = "it.user@yourcompany.com"
$sender = "it.admin@yourcompany.com"
$subject = "the process has fallen over"
$body = "there is an e-mail older than 10 minutes yourmailbox inbox."
#### query inbox of mailbox to retrieve date of oldest item
$oird = get-mailboxfolderstatistics -identity $mailbox -folderscope inbox -includeoldestandnewestitems | where-object {$_.folderpath -eq "/inbox"} | select-object -property oldestitemreceiveddate
#### create variable with above value in same format as the $date variable
$oirdinfo = $oird.oldestitemreceiveddate
#### compare the time of the oldest mail in the inbox to current time.
#### a mail is sent if more than 10 minutes has elapsed.
$compare = new-timespan -start $oirdinfo -end $date
if ($compare.minutes -gt 10) {send-mailmessage -from "$sender" -to "$recipient" -subject "$subject" -body "$body" -smtpserver "$smtpserver"}
something this:
####################################################################### # author : tom anderson - http://tomandersonpro.net # date : 18-03-2013 # comment : script checks oldest item receive date mails # : in specified mailbox. if mail older 10 minutes # : present e-mail notification sent. # history : ####################################################################### #### set current date , correct format $date = (get-date).tostring('g') #### e-mail variables $smtpserver = "smtp.yourcompany.com" $recipient = "it.user@yourcompany.com" $sender = "it.admin@yourcompany.com" $subject = "the process has fallen over" $body = "there e-mail older 10 minutes yourmailbox inbox." $mailboxes = 'mailbox1','mailbox2','mailbox3' foreach ($mailbox in $mailboxes) { #### query inbox of mailbox retrieve date of oldest item $oird = get-mailboxfolderstatistics -identity $mailbox -folderscope inbox -includeoldestandnewestitems | where-object {$_.folderpath -eq "/inbox"} | select-object -property oldestitemreceiveddate #### create variable above value in same format $date variable $oirdinfo = $oird.oldestitemreceiveddate #### compare time of oldest mail in inbox current time. #### mail sent if more 10 minutes has elapsed. $compare = new-timespan -start $oirdinfo -end $date if ($compare.minutes -gt 10) {send-mailmessage -from "$sender" -to "$recipient" -subject "$subject" -body "$body" -smtpserver "$smtpserver"} }
note: haven't tested of code; it's copied , pasted post, exception of adding $mailboxes array , changing $mailbox part of foreach loop.
Windows Server > Windows PowerShell
Comments
Post a Comment