Reputation: 33
I have been having some issues with this specific if
and else
statement in PowerShell. This is the first time I am attempting an if
and else
statement, so please be nice.
$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
$Trend_OUT_ofdate_SEND_EMAIL = 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_UP_TO_DATE_SEND_EMAIL = 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>
if (($Trend_virus_definition_up_of_date.LastWriteTime) -eq (get-date).AddDays(-3)) {
$Trend_UP_TO_DATE_SEND_EMAIL
} else {
$Trend_OUT_ofdate_SEND_EMAIL
}
I have been trying to get this to work and just seem to be missing something basic.
Upvotes: 1
Views: 680
Reputation: 200453
You're overcomplicating things. The statement
Get-Item '\\<folder location>\<specific file.*>' | where {
($_.FullName -match "<specific file.*>") –and
($_.LastWriteTime -ge ((get-date).AddDays(-3)))
} | select LastWriteTime -Last 1
could be simplified to
Get-Item '\\<folder location>\<specific file.*>' | where {
$_.LastWriteTime -ge ((get-date).AddDays(-3))
} | select LastWriteTime -Last 1
because there's no need to filter for the same name twice (both in Get-Item
and Where-Object
).
The statement will return a date when the file was updated less then 3 days ago, otherwise the result will be $null
. Thus you can use the returned value to decide which mail to send (PowerShell will interpret $null
as $false
in a boolean expression):
$definitionUpToDate = Get-Item '\\<folder location>\<specific file.*>' | where {
$_.LastWriteTime -ge ((get-date).AddDays(-3))
} | select LastWriteTime -Last 1
if ($definitionUpToDate) {
$msg = "Virus definitions up to date."
} else {
$msg = "Virus definitions not up to date."
}
Send-MailMessage -Subject $msg -Body $msg -From ...
If you want to include the date of the last update in the message you could separate fetching the date from the check:
$lastUpdate = Get-Item '\\<folder location>\<specific file.*>' |
select LastWriteTime -Last 1
if ($lastUpdate -ge (Get-Date).AddDays(-3))) {
$msg = "Virus definitions up to date."
} else {
$msg = "Virus definitions last update on $lastUpdate."
}
Send-MailMessage -Subject $msg -Body $msg -From ...
Upvotes: 1
Reputation: 58991
You realy miss some powershell basics. $Trend_OUT_ofdate_SEND_EMAIL = Send.... is a variable assignment und if you execute $Trend_OUT_ofdate_SEND_EMAIL in your if statement, you will just receive the result of the previous call. Thus, you have to call the Send-MailMessage function within your IF / ELSE statement:
$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>
}
Upvotes: 0