(415)不支持的媒体类型:相同的脚本可在我的笔记本电脑上使用,而不能在天蓝色的运行本上使用((415)不支持的媒体类型)

问题描述 投票:0回答:1

我正在使用Powershell脚本发送带有某些链接的电子邮件。该脚本在我的笔记本电脑上运行良好这里是接收到的邮件的屏幕截图]

received message

但是从Azure Powershell自动化运行手册运行时出现错误这里的错误信息:

    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 = '[email protected]'
  fromEmailAddress = '[email protected]'
  subject          = $mySubject
  contentType      = 'text/html'
  contentBody      = $htmlBody
  }
Send-SendGridEmail @splat

例如,在蔚蓝的Powershell Runbook上,将html正文减少到仅一行时,就发生这种情况

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

确实有效。

html azure powershell sendgrid
1个回答
0
投票

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

这会使Authorization标头之后的所有标头被视为消息正文,并被有效地忽略。通过简单的trim传递即可解决此问题:

$apiKey = trim($apiKey);

有关更多详细信息,您可以参考此issue

© www.soinside.com 2019 - 2024. All rights reserved.