Sending information via emails from scripts with help of AI

BEKAHENI
BEKAHENI Member Posts: 15 ✭✭

Hi All,

Thought I would try and give back since you guys have helped me in the past.

I have created a script (With the help of AI of course!!) that will send an email out of which the body can be populated with any info that you should desire.

So far I have used it for Disk Health, List All local ID's, WIndows Defender Status, all of which can be triggered by thresholds, manually etc

I first ask the AI for the info I want, using this example "Can you list all the local ID's as a table"

Then I just say can you now take the above script and combine it with another script that emails the information out (I then paste the script thats sned a blank email, see the code below)

It then gives me a working powershell script that sends an email with your desired info that can be triggered via threshodls, manually etc

Hope it helps someone :)

For me I use the "SendBlue" API, but I am sure it can be modified to send via your own SMPT

Nick

Below is the email sending powershell script (With Blank message body, use your own desired info for the message body)



# Get hostname
$hostname = [System.Net.Dns]::GetHostName()

# Get current date and time
$currentDateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

# Brevo API Configuration
$ApiKey = "PUT YOUR API KEY HERE"
$ApiUrl = "https://api.sendinblue.com/v3/smtp/email"
$headers = @{
"api-key" = $ApiKey
"Content-Type" = "application/json"
}

# Email Configuration
$Subject = "Atera Notification" # Subject of the email
$From = "PUT YOUR EMAIL HERE" # From whom we are sending an email
$To = "PUT WHERE YOUR SENDING HERE" # To whom we are sending the email

# Create the email body with the date and time stamp, hostname. THIS IS WHERE YOU CAN ADD YOUR OWN INFO
$Body = @"
<p>Date and Time: $currentDateTime</p>
<p>Atera Notification</p>
<p>Hostname: $hostname</p>
"@

# Create payload for the API
$payload = @{
sender = @{
email = $From
}
to = @(
@{
email = $To
}
)
subject = $Subject
htmlContent = $Body
} | ConvertTo-Json

# Ensure TLS 1.2 is used
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Send the email using the Brevo API
try {
$response = Invoke-RestMethod -Uri $ApiUrl -Method Post -Headers $headers -Body $payload

# Check the response from the Brevo API
if ($response -and $response.messageId) {
Write-Host "Email sent successfully. Message ID: $($response.messageId)"
} else {
Write-Host "Error sending email. No message ID received."
Write-Host "Response: $($response | ConvertTo-Json -Depth 5)"
}
} catch {
Write-Host "Error: $($_.Exception.Message)"
Write-Host "Details: $($_ | ConvertTo-Json -Depth 5)"
}

Tagged: