Я использую скрипт powershell для отправки электронной почты с некоторыми ссылками на теле. Скрипт хорошо работает на моем ноутбуке. Вот снимок экрана с тем, как выглядит полученное письмо
полученное сообщение
Однако при запуске из Azure powershell автоматизация выдает ошибку runbook Вот сообщение об ошибке:
Send-SendGridEmail : Error with Invoke-RestMethod The remote server returned an error: (415) Unsupported Media Type.
At line:176 char:1
+ Send-SendGridEmail @splat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Send-SendGridEmail
### Inside catch ###
## ErrorMessage ##
The remote server returned an error: (415) Unsupported Media Type.
## FailedItem ##
## result2 ##
CanTimeout : True
ReadTimeout : -1
WriteTimeout : -1
CanRead : True
CanSeek : True
CanWrite : True
Capacity : 256
Length : 156
Position : 156
## reader ##
CurrentEncoding BaseStream EndOfStream
--------------- ---------- -----------
System.Text.UTF8Encoding System.Net.SyncMemoryStream True
## responseBody ##
Вот мой код:
function Send-SendGridEmail {
param(
[Parameter(Mandatory = $true)]
[String] $destEmailAddress,
[Parameter(Mandatory = $true)]
[String] $fromEmailAddress,
[Parameter(Mandatory = $true)]
[String] $subject,
[Parameter(Mandatory = $false)]
[string]$contentType = 'text/html',
[Parameter(Mandatory = $true)]
[String] $contentBody
)
<#
.Synopsis
Function to send email with SendGrid
.Description
A function to send a text or HTML based email
See https://sendgrid.com/docs/API_Reference/api_v3.html for API details
This script provided as-is with no warranty. Test it before you trust it.
www.ciraltos.com
.Parameter apiKey
The SendGrid API key associated with your account
.Parameter destEmailAddress
The destination email address
.Parameter fromEmailAddress
The from email address
.Parameter subject
Email subject
.Parameter type
The content type, values are “text/plain” or “text/html”. "text/plain" set by default
.Parameter content
The content that you'd like to send
.Example
Send-SendGridEmail
#>
############ Update with your SendGrid API Key ####################
$apiKey = "APIKeyGoesHere"
$headers = @{
'Authorization' = 'Bearer ' + $apiKey
'Content-Type' = 'application/json'
'Content-transfer-encoding' = 'quoted-printable'
#'Content-transfer-encoding' = '7bit'
}
$body = @{
personalizations = @(
@{
to = @(
@{
email = $destEmailAddress
}
)
}
)
from = @{
email = $fromEmailAddress
}
subject = $subject
content = @(
@{
type = $contentType
value = $contentBody
}
)
}
try {
$bodyJson = $body | ConvertTo-Json -Depth 4
Write-Host $bodyJson
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error converting body to json ' + $ErrorMessage)
Break
}
try {
Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error with Invoke-RestMethod ' + $ErrorMessage)
echo '### Inside catch ###'
$ErrorMessage = $_.Exception.Message
echo '## ErrorMessage ##' $ErrorMessage
$FailedItem = $_.Exception.ItemName
echo '## FailedItem ##' $FailedItem
$result = $_.Exception.Response.GetResponseStream()
echo '## result2 ##' $result
$reader = New-Object System.IO.StreamReader($result)
echo '## reader ##' $reader
$responseBody = $reader.ReadToEnd();
echo '## responseBody ##' $responseBody
Break
}
}
$Link1 = "http://www.google.com"
$Link2 = "http://www.google.com"
$Link3 = "http://www.google.com"
$LienBlob = "https://portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2Fe84d34eb-168f-4678-ac5b-1cee0d6b9666%2FresourceGroups%2Frg-prd-weu-itadmin%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fsastditadminwesteuprd/path/rbac-reports/etag/%220x8D7EC1F1F6E014B%22"
$htmlBody = @"
<table>
<tr>
<td align="left">
<p>Hello,</p>
<p>Test message</p>
<p><a href=$Link1>Link 1</a></p>
<p><a href=$Link2>Link 1</a></p>
<p><a href=$Link3>Link 1</a></p>
<p>Thank you,<br /><font size="-1"><i>Automatic message. Please do not reply.</i></font></p>
</td>
</tr>
</table>
"@
$mySubject = "TesMail"
$splat = @{
destEmailAddress = 'maatoug.wassim@gmail.com'
fromEmailAddress = 'donotreply@my.net'
subject = $mySubject
contentType = 'text/html'
contentBody = $htmlBody
}
Send-SendGridEmail @splat
Бывает, что на runbook powershell azure при уменьшении тела html до одна строка, пример
<table>
<tr>
<td align="left">
<p>Hello,</p>
<p>Test message</p>
</td>
</tr>
</table>
"@
Это работает.