email - Send mail from result (should be easy) -


i have built script check local machine certificates issuer expiring within 400.000 days (to hit when testing it) on remote server, cant figure out how result send actual email experimenting pipes after (and in) if statements doesnt seem work. have been googling quiet alot (ofcourse) i'm new powershell , code difficult @ times interpret.

i looking like; if $outputfromtheifstatement | measure-object -gt 0 { send email using $sendreportnow.}

my code:

$servers = 'testserver1,'testserver2' $threshold = 400000  $deadline = (get-date).adddays($threshold) $mailsmtp = '-smtpserver smtpserver.domain.com -port 25' $mailto = "receiversmailaddress@company.com" $mailfrom = "sendersmailaddress@company.com" $mailsubject = '-subject certifikat expiring within 90 days' $mailbody = '-body ?' $sendreportnow = 'send-mailmessage $mailsmtp $mailfrom $mailto $mailsubject' invoke-command -computername $servers { get-childitem cert:\localmachine\my } | foreach {  if ($_.notafter -le $deadline -and $_.issuer -like '*certaincompanyname*') { $_ | select @{label="servername";expression={($_.pscomputername)}}, issuer, subject, @{label="expiry date & time";expression={($_.notafter)}}, @{label="expires in (days)";expression={($_.notafter - (get-date)).days}} } } 

i cleaned code up, easier digest.

$servers = 'testserver1','testserver2' $threshold = 400000  $deadline = (get-date).adddays($threshold)  $certs = invoke-command -computername $servers { get-childitem cert:\localmachine\my }   foreach ($cert in $certs){     if ($cert.notafter -le $deadline -and $cert.issuer -like '*certaincompanyname*') {          $body = $cert | select @{label="servername";expression={($cert.pscomputername)}}, issuer, subject, @{label="expiry date & time";expression={($cert.notafter)}}, @{label="expires in (days)";expression={($cert.notafter - (get-date)).days}}         $mailsubject = "[$($cert.pscomputername)] certifikat expiring within 90 days"         send-mailmessage -smtpserver smtpserver.domain.com -port 25 -from sendersmailaddress@company.com -to receiversmailaddress@company.com -subject $mailsubject -body $body     } } 

this result in e-mails being sent this:

title:              [servera] certifikat expiring within 90 days -----  body------- servername         : servera issuer             : cn=localhost subject            : cn=localhost expiry date & time : 4/5/2020 8:00:00 pm expires in (days)  : 1627 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -