Powershell 检查文件是否在3天内更新,并在与特定日期匹配时发送电子邮件

Powershell 检查文件是否在3天内更新,并在与特定日期匹配时发送电子邮件,powershell,email,Powershell,Email,我对PowerShell中的这个特定的if和else语句一直有一些问题。这是我第一次尝试使用if和else语句,所以请保持友好 $Trend\u virus\u definition\u up\u of\u date=获取项目'\\\\'| 其中{($\.FullName-match“”)–和 ($\ LastWriteTime-ge((get date).AddDays(-3)))| 选择LastWriteTime-Last 1 $Trend\u virus\u definition\u ou

我对PowerShell中的这个特定的
if
else
语句一直有一些问题。这是我第一次尝试使用
if
else
语句,所以请保持友好

$Trend\u virus\u definition\u up\u of\u date=获取项目'\\\\'|
其中{($\.FullName-match“”)–和
($\ LastWriteTime-ge((get date).AddDays(-3)))|
选择LastWriteTime-Last 1
$Trend\u virus\u definition\u out\u to\u date=获取项目'\\\\\'|
其中{($\.FullName-match“”)–和
($\ LastWriteTime-le((get date).AddDays(-3)))|
选择LastWriteTime-Last 1
$trend\u EMAIL\u up\u to\u date\u EMAIL\u body=$trend\u virus\u definition\u up\u of date
$Trend\u EMAIL\u NOT\u update\u body=$Trend\u virus\u definition\u out\u to date
$Trend\u OUT\u of date\u SEND\u EMAIL=SEND MailMessage-From-SmtpServer“smtp”`
-正文“自“$Trend\u EMAIL\u not\u update\u Body”以来,趋势病毒定义未更新”`
-主题“自“$Trend\u EMAIL\u not\u update\u body”以来,趋势病毒定义未更新”`
-到
$Trend\u UP\u TO\u DATE\u SEND\u EMAIL=SEND MailMessage-From-SmtpServer“smtp”`
-正文“趋势病毒定义最近一次更新为“$Trend\u EMAIL\u up\u date\u EMAIL\u Body”`
-主题“最新趋势病毒定义-上次更新“$Trend\u EMAIL\u UP\u to\u date\u EMAIL\u body”`
-到
if($Trend\u virus\u definition\u up\u of\u date.LastWriteTime)-eq(get date.AddDays(-3)){
$Trend\u UP\u最新\u发送\u电子邮件
}否则{
$Trend\u OUT\u of Date\u SEND\u电子邮件
}

我一直在努力让它发挥作用,但似乎缺少了一些基本的功能。

您确实错过了一些powershell的基本功能$趋势显示日期发送电子邮件=发送。。。。是一个变量赋值如果您在if语句中执行$Trend\u OUT\u of date\u SEND\u EMAIL,您将只收到上一次调用的结果。因此,您必须在IF/ELSE语句中调用Send MailMessage函数:

$Trend_virus_definition_up_of_date = Get-Item '\\<folder location>\<specific file.*>' | where {($_.FullName -match "<specific file.*>") –and ($_.LastWriteTime -ge ((get-date).AddDays(-3)))} | select LastWriteTime -Last 1

$Trend_virus_definition_out_to_date = Get-Item '\\<folder location>\<specific file.*>' | where {($_.FullName -match "<specific file.*>") –and ($_.LastWriteTime -le ((get-date).AddDays(-3)))} | select LastWriteTime -Last 1 

$trend_EMAIL_up_to_date_email_body = $Trend_virus_definition_up_of_date  
$Trend_EMAIL_NOT_up_to_date_body = $Trend_virus_definition_out_to_date  


if (($Trend_virus_definition_up_of_date.LastWriteTime) -eq (get-date).AddDays(-3))  
{
    Send-MailMessage -From <email address> -SmtpServer "smtp" -Body "Trend Virus Definitions were last updated '$trend_EMAIL_up_to_date_email_body'" -Subject "Trend Virus Definitions UP to date - Last Update '$trend_EMAIL_up_to_date_email_body'" -To <email address>
} 
else 
{
    Send-MailMessage -From <email address> -SmtpServer "smtp" -Body "Trend Virus Definitions have not been updated since '$Trend_EMAIL_NOT_up_to_date_body'" -Subject "Trend Virus Definitions have not been updated since '$Trend_EMAIL_NOT_up_to_date_body'" -To <email address>
}
$Trend\u virus\u definition\u up\u of\u date=获取项目“\\\\”;其中{($\.FullName-match“”)-和($\.LastWriteTime-ge((Get date).AddDays(-3))}选择LastWriteTime-Last 1
$Trend\u virus\u definition\u out\u to\u date=获取项“\\\\”;其中{($\.FullName-match“”)-和($\.LastWriteTime-le((Get date.AddDays(-3)))}选择LastWriteTime-Last 1
$trend\u EMAIL\u up\u to\u date\u EMAIL\u body=$trend\u virus\u definition\u up\u of date
$Trend\u EMAIL\u NOT\u update\u body=$Trend\u virus\u definition\u out\u to date
if($Trend\u virus\u definition\u up\u of\u date.LastWriteTime)-eq(get date.AddDays(-3))
{
发送邮件-发件人-SmtpServer“smtp”-正文“趋势病毒定义上次更新”$Trend\u EMAIL\u up\u date\u EMAIL\u Body'”-主题“趋势病毒定义最新-上次更新”$Trend\u EMAIL\u up\u date\u EMAIL\u Body'-至
} 
其他的
{
发送邮件-发件人-SmtpServer“smtp”-正文“自“$Trend\u EMAIL\u not\u up\u to\u date\u Body”以来趋势病毒定义未更新-主题“自“$Trend\u EMAIL\u not\u up\u to\u date\u Body”以来趋势病毒定义未更新-至
}

你把事情复杂化了。声明

Get Item'\\\\\\\\\\;其中{
($全名-匹配“”)-和
($\ LastWriteTime-ge((get date).AddDays(-3)))
}|选择LastWriteTime-Last 1
可以简化为

Get Item'\\\\\\\\\\;其中{
$\ u.LastWriteTime-ge((获取日期).AddDays(-3))
}|选择LastWriteTime-Last 1
因为不需要对同一名称进行两次筛选(在
Get Item
Where Object
中)

该语句将返回文件更新不到3天前的日期,否则结果将为
$null
。因此,您可以使用返回的值来决定发送哪封邮件(PowerShell将在布尔表达式中将
$null
解释为
$false
):

$definitionUpdate=Get Item'\\\\\\\\\\;其中{
$\ u.LastWriteTime-ge((获取日期).AddDays(-3))
}|选择LastWriteTime-Last 1
如果($DefinitionUpdate){
$msg=“病毒定义最新。”
}否则{
$msg=“病毒定义不是最新的。”
}
发送邮件-主题$msg-正文$msg-发件人。。。
如果要在消息中包含上次更新的日期,可以将日期与支票分开提取:

$lastUpdate=获取项目'\\\'|
选择LastWriteTime-Last 1
if($lastUpdate-ge(获取日期).AddDays(-3))){
$msg=“病毒定义最新。”
}否则{
$msg=“病毒定义上次更新时间为$lastUpdate。”
}
发送邮件-主题$msg-正文$msg-发件人。。。

代码有什么作用吗?如果有输出你可以张贴,请做。嗨。是的,它会为IF和ELSE向我发送两封电子邮件,但是Ansgar Wiechers的回答是正确的,而且它工作得非常完美:)Hi JisaakI真的非常同意,我每天都使用powershell,但更多的是基于命令。非常感谢您的帮助,我会重新开始,努力提高ISE水平。再次感谢Ansgar Wiechers。我真的很感激!这是一个巨大的帮助。我同意下面的评论,即我需要正确掌握ISE的基本知识。然而,在这种情况下,你是一个真正的传奇!这很好用!