Skip to content

Instantly share code, notes, and snippets.

@lukemurraynz
Created December 28, 2024 20:10
Show Gist options
  • Save lukemurraynz/3788b8e6e559f6400ffac6f05f01b1f8 to your computer and use it in GitHub Desktop.
Save lukemurraynz/3788b8e6e559f6400ffac6f05f01b1f8 to your computer and use it in GitHub Desktop.
$ClientId = 'bl012f8f-58d1-43h5-9383-bd4d104ffe27'
$emailSubject = "Important: Server Maintenance Notification"
$EmailRecipient = "[email protected]"
$SenderAddress = '[email protected]'
$emailBody = @"
<html>
<body>
<p>Dear User,</p>
<p>This is to inform you that a <b><i>server maintenance is scheduled for the next week</i></b>.</p>
<p>The servers will be down from 10:00 PM to 2:00 AM.</p>
<p>Please save your work and log off during this period to avoid any data loss.</p>
<p>If you have any questions or concerns, please contact our IT Support team.</p>
<p>Thank you for your understanding and cooperation.</p>
<p>Best Regards,</p>
<p>IT Support Team</p>
</body>
</html>
"@
if ($emailBody -ne "") {
Write-Output $emailBody
# Define the resource ID for Azure Communication Services
$ResourceID = 'https://communication.azure.com'
# Define the communication endpoint URL
$communicationendpointurl = "commserviceslukeuserassignedtest.australia.communication.azure.com" # Update with your communication endpoint URL
# Construct the URI for the identity endpoint
$Uri = "$($env:IDENTITY_ENDPOINT)?api-version=2018-02-01&resource=$ResourceID&client_id=$ClientId"
# Debug output
# Print the constructed URI and headers
Write-Output "URI: $Uri"
Write-Output "Headers: @{ Metadata = 'true' }"
# Try to get the access token
try {
# Invoke a GET request to the identity endpoint to get the access token
$AzToken = Invoke-WebRequest -Uri $Uri -Method GET -Headers @{ Metadata = "true" } -UseBasicParsing | Select-Object -ExpandProperty Content | ConvertFrom-Json | Select-Object -ExpandProperty access_token
# Print the obtained access token
Write-Output "Access Token: $AzToken"
}
catch {
# If there's an error, print the error message and response details
Write-Error "Failed to get access token: $_"
Write-Output "Response Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Output "Response Status Description: $($_.Exception.Response.StatusDescription)"
Write-Output "Response Content: $($_.Exception.Response.GetResponseStream() | %{ $_.ReadToEnd() })"
}
# Construct the URI for the email sending endpoint
$uri = "https://$communicationendpointurl/emails:send?api-version=2023-03-31"
# Define the headers for the REST API call
# Include the content type and the obtained access token in the Authorization header
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $AzToken"
}
# Define the body for the REST API call
$apiResponse = @{
headers = @{
id = (New-Guid).Guid
}
senderAddress = $SenderAddress
content = @{
subject = $emailSubject
html = $emailBody
}
recipients = @{
to = @(
@{
address = $EmailRecipient
displayName = $EmailRecipient
}
)
}
replyTo = @(
@{
address = "[email protected]"
displayName = "Contoso"
}
)
userEngagementTrackingDisabled = $true
}
# Convert the PowerShell object to JSON
$body = $apiResponse | ConvertTo-Json -Depth 10
# Send the email
try {
# Log the request details
Write-Output "Sending email..."
Write-Output "URI: $uri"
Write-Output "Headers: $headers"
Write-Output "Body: $body"
# Make the request
$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body -UseBasicParsing
# Log the response
Write-Output "Response: $response"
# Return the response
$response
}
catch {
# Log the error
Write-Error "Failed to send email: $_"
Write-Output "Exception Message: $($_.Exception.Message)"
Write-Output "Exception StackTrace: $($_.Exception.StackTrace)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment