Home > Software design >  Powershell - Send email when number of days reaches a set interval
Powershell - Send email when number of days reaches a set interval

Time:08-19

I'm at a blocker and need some help!

(New-Timespan -Start (Get-Date) -End (Get-AdfsCertificate -CertificateType Token-Signing | where-object { $_.IsPrimary -eq $true }).Certificate.NotAfter).Days

This returns a result of days until it is no longer a primary certificate. E.G simply "141"

How can I implement a way of looking at this result and sending an email when it reaches a certain day? E.G 20 days until it is no longer primary.

I can use a mail relay, so no credentials are required!

Many thanks in advance!

CodePudding user response:

Put your code in a Task Schedule to run on a date and time, use an if/then code block to check the state, and send as needed when it meets your metric.

For example:

If ((New-Timespan -Start (Get-Date) -End (Get-AdfsCertificate -CertificateType Token-Signing | 
where-object { $PSItem.IsPrimary -eq $true }).Certificate.NotAfter).Days -eq 20)
{
    # Do something here
}

As for email, this is why the Send-Email cmdlet exists.

Get-Help -Name Send-MailMessage -Full
Get-Help -Name Send-MailMessage -Examples

# Results
<#
NAME
    Send-MailMessage
    
SYNOPSIS
    Sends an email message.

    
    
    # -- Example 1: Send an email from one person to another person --
    
    Send-MailMessage -From 'User01 <[email protected]>' -To 'User02 <[email protected]>' -Subject 'Test mail'
    
    The `Send-MailMessage` cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipient. The Subject 
    parameter uses the text string Test mail as the message because the optional Body parameter is not included.
    
    
    # ---------------- Example 2: Send an attachment ----------------
    
    Send-MailMessage -From 'User01 <[email protected]>' -To 'User02 <[email protected]>', 'User03 <[email protected]>' -Subject 'Sending the 
    Attachment' -Body "Forgot to send the attachment. Sending now." -Attachments .\data.csv -Priority High -DeliveryNotificationOption OnSuccess, 
    OnFailure -SmtpServer 'smtp.fabrikam.com'
    
    The `Send-MailMessage` cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipients. The 
    Subject parameter describes the content of the message. The Body parameter is the content of the message.
    
    The Attachments parameter specifies the file in the current directory that is attached to the email message. The Priority parameter sets the message 
    to High priority. The -DeliveryNotificationOption parameter specifies two values, OnSuccess and OnFailure . The sender will receive email 
    notifications to confirm the success or failure of the message delivery. The SmtpServer parameter sets the SMTP server to smtp.fabrikam.com .
    
    
    # ----------- Example 3: Send email to a mailing list -----------
    
    Send-MailMessage -From 'User01 <[email protected]>' -To 'ITGroup <[email protected]>' -Cc 'User02 <[email protected]>' -Bcc 'ITMgr 
    <[email protected]>' -Subject "Don't forget today's meeting!" -Credential domain01\admin01 -UseSsl
    
    The `Send-MailMessage` cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipients. The Cc 
    parameter sends a copy of the message to the specified recipient. The Bcc parameter sends a blind copy of the message. A blind copy is an email 
    address that is hidden from the other recipients. The Subject parameter is the message because the optional Body parameter is not included.
    
    The Credential parameter specifies a domain administrator's credentials are used to send the message. The UseSsl parameter specifies that Secure 
    Socket Layer (SSL) creates a secure connection.
#>
  • Related