Powershell - 添加 CC 和 BCC 时出错 - 哈希文本中的键后缺少“=”

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

此脚本用于在 SendGrid 中使用发送邮件,代码是用 powershell 脚本编写的。 我尝试在下面的代码中添加 CC 和 BCC,但它没有按预期工作。

param
(
    [Parameter(Mandatory=$false)]
    [object] $WebhookData
)

write-output "start"
write-output ("object type: {0}" -f $WebhookData.gettype())
write-output $WebhookData
write-output "`n`n"

if ($WebhookData.RequestBody) {
    $details = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)

    foreach ($x in $details)
    {
        $destEmailAddress = $x.destEmailAddress
        Write-Output "To - $destEmailAddress"

        $fromEmailAddress = $x.fromEmailAddress
        Write-Output "From - $fromEmailAddress"

        $subject = $x.subject
        Write-Output "Subject Line - $subject"

        $content = $x.content
        Write-Output "Mail Body - $content"

        # Optional CC and BCC as arrays
        $ccEmailAddresses = $x.ccEmailAddress

        $bccEmailAddresses = $x.bccEmailAddress
    }
}
else {
    Write-Output "No details received"
}

Disable-AzContextAutosave -Scope Process

$SENDGRID_API_KEY = "XYZ"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " + $SENDGRID_API_KEY)
$headers.Add("Content-Type", "application/json")

$body = @{
    personalizations = @(
        @{
            to = @(
                @{
                    email = $destEmailAddress
                }
            )
        }
    )
}

if ($ccEmailAddresses) {
    $cc = @()
    foreach ($ccAddress in $ccEmailAddresses) {
        $cc += @{
            email = $ccAddress
        }
    }
    $body.personalizations[0].cc = $cc
}

if ($bccEmailAddresses) {
    $bcc = @()
    foreach ($bccAddress in $bccEmailAddresses) {
        $bcc += @{
            email = $bccAddress
        }
    }
    $body.personalizations[0].bcc = $bcc
}

$body.from = @{
    email = $fromEmailAddress
}
$body.subject = $subject
$body.content = @(
    @{
        type = "text/html"
        value = $content
    }
)

$bodyJson = $body | ConvertTo-Json -Depth 4

$response = Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson

但下面是我得到的错误。我似乎无法弄清楚需要改变什么。短暂性脑缺血发作。

行:60 字符:11 + if ($ccEmailAddress) { + ~ 缺少 '=' 运算符 在哈希文本中的键之后。在行:71 字符:9 + } + ~ 缺少结束语 子表达式中的“)”。在行:72 字符:5 + } + ~ 中缺少结束符 ')' 子表达式。在行:73 字符:1 + ) + ~ 中出现意外标记 ')' 表达或陈述。在第 74 行 字符:1 + } + ~ 意外标记 表达式或语句中的“}”。在行:76 字符:1 + from = @{ + ~~~~ 此版本的语言不支持“from”关键字。 在第 86 行 字符:1 + } + ~ 表达式或中出现意外标记“}” 声明。

我传递给该脚本的 JSON 如下。 CC 和 BCC 作为数组传递。

               {'destEmailAddress' : '[email protected]',
                    'fromEmailAddress' : '[email protected]',
                    'subject' : 'mailSubject',
                    'content' : 'mailContent',
                    'ccEmailAddresses' : '['[email protected]','[email protected]']',
                    'bccEmailAddresses' : '['']'
                } 
azure powershell azure-powershell
1个回答
0
投票

PowerShell - 添加 CC 和 BCC 时出错 - 哈希文本中的键后缺少“=”:-

根据错误消息,我发现您的脚本存在一些问题,详情如下。

  • 检查脚本中的括号并删除多余的括号,因为错误显示了意外的令牌消息。
  • 哈希文本中的键后缺少“=”:在
    =
    bc
    键后添加
    cc
    运算符。
  • 还迭代数组
    $ccEmailAddresses
    $bccEmailAddresses
    为每个电子邮件地址生成对象。

修改后的PowerShell脚本:

param
(
    [Parameter(Mandatory=$false)]
    [object] $WebhookData = "xxxx"
)

write-output "start"
write-output ("object type: {0}" -f $WebhookData.gettype())
write-output $WebhookData
write-output "`n`n"

if ($WebhookData.RequestBody) {
    $details = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)

    foreach ($x in $details)
    {
        $destEmailAddress = $x.destEmailAddress
        Write-Output "To - $destEmailAddress"

        $fromEmailAddress = $x.fromEmailAddress
        Write-Output "From - $fromEmailAddress"

        $subject = $x.subject
        Write-Output "Subject Line - $subject"

        $content = $x.content
        Write-Output "Mail Body - $content"

        # Optional CC and BCC as arrays
        $ccEmailAddresses = $x.ccEmailAddress

        $bccEmailAddresses = $x.bccEmailAddress
    }
}
else {
    Write-Output "No details received"
}

Disable-AzContextAutosave -Scope Process

$SENDGRID_API_KEY = "XYZ"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " + $SENDGRID_API_KEY)
$headers.Add("Content-Type", "application/json")
$body = @{
    personalizations = @(
        @{
            to = @(
                @{
                    email = $destEmailAddress
                }
            )
            cc = @($cc)  
            bcc = @($bcc) 
        }
    )
    
    subject = $subject
    content = @(
        @{
            type = "text/html"
            value = $content
        }
    )
}

$bodyJson = $body | ConvertTo-Json -Depth 4
$response = Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson

由于我的 webhook 数据变量中没有太多数据,因此 abvoe 脚本已成功执行,但结果较少。

enter image description here

更新

$cc 和 $bcc 被定义为一个空数组,用于依次存储值,并在主脚本中引用它,如上所述。

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