dimanche 25 juin 2017

Proper use of If Comparison Statements for Select-String

I'm writing an site monitoring script, but am having trouble with some If statements.

Function mail 
    {
    $Report = Get-Content C:\Users\proto\Desktop\ITServiceCheck\Down.txt

if  ($report -contains "http")
    {
    Write-Host -ForegroundColor cyan "Check Complete, preparing email"
    Sleep 5 
    $EmailFrom = "myemail" 
    $EmailTo = "emailto" 
    $SMTPServer = "smtp.gmail.com" 
    $EmailSubject = "Services Monitoring Result"  

    #Send mail with output 
    $mailmessage = New-Object system.net.mail.mailmessage  
    $mailmessage.from = ($EmailFrom)  
    $mailmessage.To.add($EmailTo) 
    $mailmessage.Subject = $EmailSubject 
    $mailmessage.Body = (Get-Content C:\Users\proto\Desktop\ITServiceCheck\Down.txt | Select-String "http")
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer,587)   
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myemail", "myemailpassword");  
    $SMTPClient.EnableSsl = $true  
    $SMTPClient.Send($mailmessage)
    sleep 5
    Write-Host -ForegroundColor Cyan "Cleaning up Down.txt"
    Clear-Content .\down.txt
    }
    sleep 10


if  ($report -notmatch "http")
    {
    Write-Host -ForegroundColor Cyan "All okay, going to sleep"
    Sleep 10
    }
    }

    mail

If Down.txt contains any string with "http" I want it to send out an email, if Down.txt doesnt contain any string with "http" I just want it to say " All is okay " As you can see I've been trying to use -contains and -notmatch, but no luck, I know my if statements are wrong, but Im not sure, what to put instead. Can someone explain what Im doing wrong

`

Aucun commentaire:

Enregistrer un commentaire