获取电子邮件通知文件夹计数

时间:2016-03-07 21:34:03

标签: powershell scripting

使用这个powershell脚本,我可以在文件夹中获取文件数

Get-ChildItem D:\ -Recurse -File | Measure-Object | %{$_.Count} 

这是发送电子邮件的脚本。

$Count = Get-ChildItem D:\ -Recurse -File | Measure-Object | %{$_.Count} 
$Status= $Event.Message 
$From = "sender@sender.com"
$To = "receiver"
$SMTPServer = "smtp"
$SMTPPort = "587"
$Username = "email@email.us"
$Password = "password"
$Subject = "$computer,Files count"
$Body = "Number of Files is $Count |D drive test "
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $false
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($From, $To, $subject, $body); 
$computer = "$env:computername

但是输出只是纯文本文件数是58 | D驱动器测试

我们是否可以通过更多文本来提醒我们如果文件计数不匹配,则应该考虑“文件数量是60这么大”的警告,或者如果它变得越来越少则应该是关键和错误。

2 个答案:

答案 0 :(得分:1)

你有比较运算符:

  • -lt =低于
  • -gt =大于
  • -ge =大于或等于
  • -le =小于或等于
  • -eq =等于

您有ifelseifelse

你可以改变你的身体信息(如DarkMoon的答案所示)

if ($count -le '50') {
    $Body = 'your text'
}
elseif (($count -gt '50') -and ($count -le '100')) {
    $Body = 'your text'
}
elseif (($count -gt '100') -and ($count -le '500')) {
    $Body = 'your text'
}
else {
    $Body = 'your text'
}

答案 1 :(得分:0)

对于警告,它只是简单的事情:

if ($Count -lt 60) {
  $Body = "Number of Files is low at $Count |D drive test "
} else {
  $Body = "Number of Files is $Count which is large |D drive test "
}