Powershell - Error while adding CC and BCC - Missing '=' after key in hash literal

68 Views Asked by At

This script is used to send mails using in SendGrid and code is written in powershell script. I am trying to add CC and BCC in the below code, but it is not working as expected.

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

But below is the error I get. I can't seem to figure out what needs to be changed. TIA.

At line:60 char:11 + if ($ccEmailAddress) { + ~ Missing '=' operator after key in hash literal. At line:71 char:9 + } + ~ Missing closing ')' in subexpression. At line:72 char:5 + } + ~ Missing closing ')' in subexpression. At line:73 char:1 + ) + ~ Unexpected token ')' in expression or statement. At line:74 char:1 + } + ~ Unexpected token '}' in expression or statement. At line:76 char:1 + from = @{ + ~~~~ The 'from' keyword is not supported in this version of the language. At line:86 char:1 + } + ~ Unexpected token '}' in expression or statement.

JSON I am passing to this script is as below. CC and BCC are passed as arrays.

               {'destEmailAddress' : '[email protected]',
                    'fromEmailAddress' : '[email protected]',
                    'subject' : 'mailSubject',
                    'content' : 'mailContent',
                    'ccEmailAddresses' : '['[email protected]','[email protected]']',
                    'bccEmailAddresses' : '['']'
                } 
0

There are 0 best solutions below