Send Powershell results to email using SMTP -
#build base pieces emailing results $servername = gc env:computername $smtpclient = new-object system.net.mail.smtpclient $mailmessage = new-object system.net.mail.mailmessage $mailmessage.body = "" $smtpclient.host = "mail" $mailmessage.from = ($servername + "@company.com") $mailmessage.to.add("josh.o@comapany.com") $mailmessage.subject = $servername + " certificate expiration results" $threshold = 300 #number of days expiring certificates $deadline = (get-date).adddays($threshold) #set deadline date dir cert:\localmachine\trustedpeople | foreach { if ($_.notafter -le $deadline) { $_ | select issuer, subject, notafter, @{label="expires in (days)";expression={($_.notafter - (get-date)).days}} } }
this script checks certificate expire within specified time frame specified, want create scheduled task , make send me emails. stuck here. have idea on how make send me emails using smtp server?
function mail-output { param( [string]$subject ) $from = "emailfrom@example.com" $to = "emailto@example.com" $smtpserver = "smtp.example.com" $smtpport = "587" $username = "username" $password = "password" $body = "body of email goes here" $smtp = new-object system.net.mail.smtpclient($smtpserver, $smtpport); $smtp.enablessl = $true $smtp.credentials = new-object system.net.networkcredential($username, $password); $smtp.send($from, $to, $subject, $body); }
this use send simple smtp emails within powershell. send-mailmessage expanded show cmdlet does.
Comments
Post a Comment