【问题标题】:Powershell script for count files and folders and send email用于计数文件和文件夹并发送电子邮件的 Powershell 脚本
【发布时间】:2020-07-28 19:06:39
【问题描述】:

我找到了一个脚本来计算文件夹中的文件数(看起来不错),但由于某种原因它不起作用。目标是,如果文件夹中的文件数量超过五个,我会收到有关它的自动通知。 如果有人可以帮助我,我不是很擅长脚本,这是我找到并且不起作用的脚本:

$Output = "c:\Test\*"

If (Get-Content -Path $Output | Where-Object {$_.Count -gt10})
{
    $MailArgs = @{
            'To'          = "my@mail.com"
            'From'        = "some@mail.com"
            'Subject'     = "some text"
            'Body'        = "Number of files"
            'SmtpServer' = "smtp"
    }

    Send-MailMessage @MailArgs
    
}

【问题讨论】:

  • 您应该可以将(Get-Content -Path $Output | Where-Object {$_.Count -gt10}) 替换为((Get-ChildItem $Output | Measure-Object).Count -gt 10)
  • 谢谢 Bill_Stewart,您的代码运行良好。还有一个问题,如果我想检查三个子文件夹中的文件数。是否可以在此脚本中进行一些修改?再次感谢您。
  • 查看Get-ChildItem 上的-Recurse 开关,以及-File 开关,否则该cmdlet 还会列出子文件夹

标签: powershell count smtp output


【解决方案1】:

替换这个:

(Get-Content -Path $Output | Where-Object {$_.Count -gt 10})

用这个:

((Get-ChildItem $Output | Measure-Object).Count -gt 10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多