(415)不支持的媒体类型:相同的脚本在我的笔记本电脑上有效,但在azure runbook上无效((415)媒体类型不



我正在使用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

碰巧,在azure powershell runbook上,当将html正文减少到只有一行时,例如

<table>
<tr>
<td align="left">
<p>Hello,</p>
<p>Test message</p> 
</td>
</tr>
</table>
"@

它确实有效。

发送SendGridEmail:Invoke-RestMethod错误远程服务器返回错误:(415(不支持的媒体类型。

这会导致Authorization标头之后的所有标头都被视为消息正文,并被有效地忽略。一个简单的通过trim可以解决这个问题:

$apiKey = trim($apiKey);

有关更多详细信息,您可以参考此问题。

最新更新