See attached photo (link) of Output in PowerShell and Outlook.
I am trying to send output via email but having troubles getting the script to put the output in the body of the email. Instead of grabbing the Services Names and Expiration Dates that fall in-between the desired threshold (70 days), it is grabbing everything in the CSV (ex. see a Service that expires in 2022) and putting that into the body of the email.
My question is how do I have it email just the information that I pulled in PowerShell (the ones that fall between the specified threshold date)?
Somewhat newer to scripting, thanks! :)
$now=get-date
$cert = Import-Csv C:\Users\userID\Desktop\Certificate_CSV2.csv
$threshold = 70
$deadline = (Get-Date).AddDays($threshold)
$cert | Select-Object "Service Name", "Certificate Expiration Date" | Where-Object {$_."Certificate Expiration Date" -as [datetime] -le $deadline}
Send-mailMessage -to "SentTo@email.com" -subject "test message 8" -from "SentTo@email.com" -body ($cert | Out-String) -SmtpServer pobox.email.com
You're just printing out cert with the filter, not applying it back to cert. To fix, just set $cert equal to the filtered version of itself.
$cert = $cert | Select-Object "Service Name", "Certificate Expiration Date" | Where-Object {$_."Certificate Expiration Date" -as [datetime] -le $deadline}