user3542587
user3542587

Reputation: 317

Send Email if get content of file more than 2 line

This script will be sending email to myself if more than 2 lines (3rd lines onwards). I tried on below script but not managed to get any email notification. SMTP server is working fine and no issue. May I know what problem with my code?

Tools:

  1. Using powershell v2.0
  2. Using .Net 4
  3. Window Server 2008
$Output = ".\Name.txt"

If (Get-Content -Path $Output | Where-Object {$_.Count -gt 2})
{
    $MailArgs = @{
            'To'          = "[email protected]"
            'From'        = "[email protected]"
            'Subject'     = "Pending. "
            'Attachments' =  $Output
            'Body'        = "Please close it"

            'SmtpServer' = "exchangeserver.com"
    }

    Send-MailMessage @MailArgs
}

Example of Output File will send email

| Name | PassportNo |    DOB     |                                      |
+------+------------+------------+--------------------------------------+
| A    | IDN7897    | 29-08-1980 | << once got this row will send email |
| B    | ICN5877    | 14-08-1955 |                                      |
| C    | OIY7941    | 01-08-1902 |                                      |
+------+------------+------------+--------------------------------------+

Upvotes: 0

Views: 384

Answers (1)

Theo
Theo

Reputation: 61028

As commented, your If test is wrong.

Using If (Get-Content -Path $Output | Where-Object {$_.Count -gt 2}) you are piping each single line from the file and test if the .Count property on that single line is greater than 2 (which of course is never the case)

Change the If into

If ((Get-Content -Path $Output).Count -gt 2)

so the .Count property will give you the total number of lines in the file.

Upvotes: 2

Related Questions