windows 获取电子邮件通知文件计数结果为真- PowerShell

uurity8g  于 2023-01-21  发布在  Windows
关注(0)|答案(1)|浏览(126)

需要发送邮件,如果输出结果为真,但我的代码得到邮件真或假
如果是真的,它能提醒我们吗
.............................................

here is the script sending email.
Get-ChildItem D:\Infra\ -Recurse -File | Measure-Object | %{$_.Count} > c:\temp\result.txt

$Output = "D:\Infra\*"

Get-ChildItem D:\Infra\ | select name, lastwritetime, creationTime > c:\temp\file_date_time.txt
Get-date > c:\temp\server_time.txt

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

$attachments = @(
    "c:\temp\result.txt"
    "c:\temp\file_date_time.txt"
    "c:\temp\server_time.txt"
)

$mailArgs = @{
    From =          ""
    To =            ""
    Subject =       ""
    Body =          ""
    Attachments =   $attachments
    SmtpServer =    ""
    Port =          25
    UseSSL =        $true
}
Send-MailMessage @mailArgs
zwghvu4y

zwghvu4y1#

无论对象计数是否大于10,您的脚本都会执行mail部分,因为它缺少求值。

Get-ChildItem D:\Infra\ -Recurse -File | Measure-Object | %{$_.Count} > 
c:\temp\result.txt

$Output = "D:\Infra\*"

Get-ChildItem D:\Infra\ | select name, lastwritetime, creationTime > 
c:\temp\file_date_time.txt
Get-date > c:\temp\server_time.txt

#Assign to a variable so you can evaluate value in further steps (If True/false)
$CheckCount = ((Get-ChildItem $Output | Measure-Object).Count -gt 10)

#If CheckCount evaluates to $true, execute the mail section
if($CheckCount){

     $attachments = @(
       "c:\temp\result.txt"
       "c:\temp\file_date_time.txt"
       "c:\temp\server_time.txt"
     )

     $mailArgs = @{
        From =          ""
        To =            ""
        Subject =       ""
        Body =          ""
        Attachments =   $attachments
        SmtpServer =    ""
        Port =          25
        UseSSL =        $true
    }

Send-MailMessage @mailArgs
}

相关问题