i have been having issues specific if , else statement in powershell. first time attempting if , else statement, please nice.
$trend_virus_definition_up_of_date = get-item '\\<folder location>\<specific file.*>' | {($_.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.*>' | {($_.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 last updated '$trend_email_up_to_date_email_body'" ` -subject "trend virus definitions 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 work , seem missing basic.
you're overcomplicating things. statement
get-item '\\<folder location>\<specific file.*>' | { ($_.fullname -match "<specific file.*>") –and ($_.lastwritetime -ge ((get-date).adddays(-3))) } | select lastwritetime -last 1
could simplified to
get-item '\\<folder location>\<specific file.*>' | { $_.lastwritetime -ge ((get-date).adddays(-3)) } | select lastwritetime -last 1
because there's no need filter same name twice (both in get-item
, where-object
).
the statement return date when file updated less 3 days ago, otherwise result $null
. can use returned value decide mail send (powershell interpret $null
$false
in boolean expression):
$definitionuptodate = get-item '\\<folder location>\<specific file.*>' | { $_.lastwritetime -ge ((get-date).adddays(-3)) } | select lastwritetime -last 1 if ($definitionuptodate) { $msg = "virus definitions date." } else { $msg = "virus definitions not date." } send-mailmessage -subject $msg -body $msg -from ...
if want include date of last update in message separate fetching date check:
$lastupdate = get-item '\\<folder location>\<specific file.*>' | select lastwritetime -last 1 if ($lastupdate -ge (get-date).adddays(-3))) { $msg = "virus definitions date." } else { $msg = "virus definitions last update on $lastupdate." } send-mailmessage -subject $msg -body $msg -from ...
Comments
Post a Comment