powershell - 如果获取的文件内容超过 2 行,则发送电子邮件
问题描述
如果超过 2 行(从第 3 行开始),此脚本将向我自己发送电子邮件。我尝试了下面的脚本,但没有收到任何电子邮件通知。SMTP 服务器工作正常,没有问题。我可以知道我的代码有什么问题吗?
工具:
- 使用powershell v2.0
- 使用.Net 4
- 窗口服务器 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 | |
+------+------------+------------+--------------------------------------+
解决方案
如评论所述,您的 If 测试是错误的。
使用If (Get-Content -Path $Output | Where-Object {$_.Count -gt 2})
您从文件中管道传输每一行并测试该单行上的 .Count 属性是否大于 2(当然绝不是这种情况)
将如果更改为
If ((Get-Content -Path $Output).Count -gt 2)
因此 .Count 属性将为您提供文件中的总行数。
推荐阅读
- linux - bash检查文件中的所有行是否等于“数字”
- java - 如何在 BOX Api 中上传文件:空指针异常
- c++ - Vowpal Wabbit 是否支持在训练期间添加和删除功能?
- spring - Bean 名称错误的 BindingResult 和普通目标对象都不是
- swift - 如何衡量异步方法的性能?
- html - CSS 样式表适用于某些属性,但不是全部
- javascript - 在 Accordion 菜单中隐藏部分
- sql - 在 SQL Server 表的多行中查找重复的 **Set of Values**
- c# - 我编写了一个用于合并 Excel 工作表的控制台应用程序。我收到以下错误
- flutter - 使用 GoogleFonts 时 FontWeight 不起作用