首页 > 解决方案 > 如果获取的文件内容超过 2 行,则发送电子邮件

问题描述

如果超过 2 行(从第 3 行开始),此脚本将向我自己发送电子邮件。我尝试了下面的脚本,但没有收到任何电子邮件通知。SMTP 服务器工作正常,没有问题。我可以知道我的代码有什么问题吗?

工具:

  1. 使用powershell v2.0
  2. 使用.Net 4
  3. 窗口服务器 2008
$Output = ".\Name.txt"

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

            'SmtpServer' = "exchangeserver.com"
    }

    Send-MailMessage @MailArgs
}

输出文件示例将发送电子邮件

| 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 |                                      |
+------+------------+------------+--------------------------------------+

标签: powershell

解决方案


如评论所述,您的 If 测试是错误的。

使用If (Get-Content -Path $Output | Where-Object {$_.Count -gt 2})您从文件中管道传输每一行并测试该单行上的 .Count 属性是否大于 2(当然绝不是这种情况)

将如果更改为

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

因此 .Count 属性将为您提供文件中的总行数。


推荐阅读