Email notification based on file output

Is there a way to have Powershell read the contents of a text file, and if there are any numbers between 200 and 10000 (or anything over 200) on any of the lines in the file, send an email.  I will create a scheduled task to run the Powershell script against the text file every 15 minutes.
exchsvrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rastoiWindows DTS expertCommented:
simple answer is yes, but ...
do you have accessible SMTP server in your environment? What are its authorization requirementsts

this oneliner does what you need, just write-host needs to be replaced by send-mailmessage with proper paremeter set

get-content .\test.txt |
foreach {
-split (( $_ -replace '\D+'," ").trim())|
foreach {
if (([int]$_ -ge 200) -and ([int]$_ -le 10000)){
Write-host $_
}}}

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Detecting an arbitrary number in a text stream is a flaky task. The more you can restrict, the better. E.g. is there a keyword prefix or suffix, are spaces or other delimiters surrounding the numbers?
Can you post some example input?
0
rastoiWindows DTS expertCommented:
why optimalization comes to mind only later ?
the line 3:
-split (( $_ -replace '\D+'," ").trim())|

Open in new window

can be replaced by much more simple
$_ -split '\D+'|

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

exchsvrAuthor Commented:
Apologies for being so vague. I am querying an Exchange Server transport queue for message queue sizes(Please see example below), if any of the queues are over 200 messages, i would like to get an email. The query runs every 15 minutes via a scheduled task, and exports the results to a text file.  

Identity                                                   DeliveryType Status MessageCount NextHopDomain
--------                                                   ------------ ------ ------------ -------------
ServerName\1232478                                           DnsConnec... <>  13            mail.domain1.com
ServerName\5462478                                           DnsConnec... <>  442           mail.external2.com
ServerName\8362478                                           DnsConnec... <>  6             mail.externaldomain3.com
1
rastoiWindows DTS expertCommented:
I assume, that you run your query from powershell script. Why you simply not update it with message count evaluation prior export to file?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
It's much easier if you keep the info on a CSV file.
1
exchsvrAuthor Commented:
I actually see an issue where the Identity portion of the querycan be an issue, the output file results have been modified to only output the message queue size.
MessageCount
------
 13
 442      
 6  
--- I found a second script that will email based on number variables, but it does not seem to work

$File = "C:\test.txt"
$Pattern = [regex]", x[n-n][n-n],"
$MsgParam = @{
    To = "me <me@domain.com>"
    From = "Test <queuetest@test.com>"
    Subject = "Test"
    smtpServer = "internal smtp server"
}

Get-Content $File -Wait | Select-string -Pattern $Pattern | ForEach {
    Send-MailMessage @MsgParam
}
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You are loosing the server information with exporting only the message count. Better to export as CSV file with all data, like using| export-csv -notype C:\test.csv instead of out-file (or whatever you use to create the file). Then:
$MsgParam = @{
    To = "me <me@domain.com>" 
    From = "Test <queuetest@test.com>" 
    Subject = "Test" 
    smtpServer = "internal smtp server"
}

Import-CSV C:\test.csv |
  ? { $_.MessageCount -gt 200 ] |
  % { Send-MailMessage @MsgParam -Body "Server $($_.Identity) exceeded message queue count: $($_.MessageCount)" }

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
exchsvrAuthor Commented:
this works perfectly Qlemo thank you!
0
MichelangeloConsultantCommented:
Ciao,
I use a single script for such reports. It is scheduled on a Exchange server,  it checks queues and sends email if any queues' messagecount is greater than a given threshold. It can be easily adapted to be scheduled on each exchange server and to check just the queues on that server in case you need it.
As it is, it checks all exchange servers in the same site and can export results so no need to parse the text file.
# Queue check and report
# 
# exchange snapin needed to access Exchange cmdlets
add-pssnapin *exchange*

# messagecount threshold
[int]$msgcount = 200

# Exchangeservers: substitute as needed i.e. "myserver" to check a single server
# Use $exservers = $env:computername to check the host on which the script is running on
$ExServers = get-exchangeserver -ErrorAction stop

# get relevant queues for all Exchange Servers; exclude ShadowRedundancy in case there is a DAG
$queueoutput = $ExServers | get-queue -Filter { messagecount -gt $msgcount -and DeliveryType -ne "ShadowRedundancy" } 

if ( $queueoutput.count -gt 0 ) {
        $queueoutput | sort Identity,messagecount  -Descending | ForEach-Object {
        $MsgParam = @{
            To = "recipient <recipient@example.com>"
            #From = "queuereport <queuereport@example.com"
            Subject = "Server $($_.Identity) exceeded message queue count: $($_.MessageCount)"
            smtpServer = "smtprelay.example.com"
            Body =  $_ | select * | Out-String 
        }
        Send-MailMessage @MsgParam 
    }
}

#export data, comment out if not needed
$queueoutput | export-csv -path .\testqueue.txt -NoTypeInformation

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.