如何以编程方式将电子邮件收件人添加到 Graph API 请求?

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

我有一些运行手册在 Azure 自动化中运行。我正在将电子邮件通知添加到操作手册中。我配置了一个简单的运行手册来发送电子邮件。我不想在每个 Runbook 的末尾添加电子邮件“部分”,而是希望有一个可以从其他 Runbook 调用的单个“电子邮件”Runbook。我遇到了电子邮件收件人数量的问题。如何动态更改 Graph API 请求中的电子邮件收件人数量以匹配从其他 Runbook 传递的收件人数量?

这是电子邮件操作手册。正如所写,效果很好。

# Import variables from Azure Automation account
$client_id = Get-AutomationVariable -Name 'Azure Automation Emails Application Client ID'
$client_key = Get-AutomationVariable -Name 'Azure Automation Emails Application Key'
$tenant_id = Get-AutomationVariable -Name 'Azure Automation Emails Application Tenant ID'

# Get an access token to send the email
$request = @{
    Method = 'POST'
    URI    = "https://login.microsoftonline.com/$tenant_id/oauth2/v2.0/token"
    body   = @{
        grant_type    = "client_credentials"
        scope         = "https://graph.microsoft.com/.default"
        client_id     = $client_id
        client_secret = $client_key
    }
}
$token = (Invoke-RestMethod @request).access_token
# DEBUGGING - view the token value
#$token

# Provide the sender and recipient email addresses
$fromAddress = '[email protected]'
$toAddress1 = '[email protected]'
$toAddress2 = '[email protected]'

# Specify the email subject and the message
$mailSubject = 'Test email'
$mailMessage = 'this is a test'

# Build the Graph API request and send the message
$params = @{
    "URI"         = "https://graph.microsoft.com/v1.0/users/$fromAddress/sendMail"
    "Headers"     = @{
      "Authorization" = ("Bearer {0}" -F $token)
    }
    "Method"      = "POST"
    "ContentType" = 'application/json'
    "Body" = (@{
        "message" = @{
            "subject" = $mailSubject
            "body"    = @{
                "contentType" = 'Text'
                "content"     = $mailMessage
            }
            "toRecipients" = @(
                @{
                    "emailAddress" = @{
                        "address" = $toAddress1
                    }
                }
                @{
                    "emailAddress" = @{
                        "address" = $toAddress2
                    }
                }
            )
        }
    }) | ConvertTo-JSON -Depth 10
  }
  # Send the message
  Invoke-RestMethod @params -Verbose

如果我为空或空白

$toAddress2
,或者如果我没有有效的电子邮件地址,则电子邮件操作手册将会失败。我尝试将
$toAddress1
转换为数组,就像这样,
$toAddress1 = @{"address" = "[email protected]"; "address" = "[email protected]"}
,但是失败了,因为数组中不能有重复的键。

或者是在每个 Runbook 末尾简单构建 Graph API 请求参数,然后将其作为变量传递给电子邮件 Runbook 的最佳选择?

powershell microsoft-graph-api azure-automation
1个回答
0
投票

您可以使用单个数组,然后在

toRecipients
属性中使用循环来动态处理收件人:

$toAddress = '[email protected]', '[email protected]'

if ($toAddress.Count -eq 0) {
    # you should exit early here since there are no recipients
    throw 'No rescipients...'
}

$params = @{
    'URI'         = "https://graph.microsoft.com/v1.0/users/$fromAddress/sendMail"
    'Headers'     = @{
        'Authorization' = ('Bearer {0}' -F $token)
    }
    'Method'      = 'POST'
    'ContentType' = 'application/json'
    'Body'        = @{
        'message' = @{
            'subject'      = $mailSubject
            'body'         = @{
                'contentType' = 'Text'
                'content'     = $mailMessage
            }
            'toRecipients' = @(
                # here you can add a loop to create a new hashtable
                # per recipient in `$toAddress`
                $toAddress | ForEach-Object {
                    @{
                        emailAddress = @{
                            address = $_
                        }
                    }
                }
            )
        }
    } | ConvertTo-Json -Depth 10
}

您还可以在 Runbook 中设置 输入参数,而不是对收件人进行硬编码,这样您就可以在调用 Runbook 时传递它们:

param([Parameter(Mandatory)] [string[]] $Recipients)

$params = @{
    'URI'         = "https://graph.microsoft.com/v1.0/users/$fromAddress/sendMail"
    'Headers'     = @{
        'Authorization' = ('Bearer {0}' -F $token)
    }
    'Method'      = 'POST'
    'ContentType' = 'application/json'
    'Body'        = @{
        'message' = @{
            'subject'      = $mailSubject
            'body'         = @{
                'contentType' = 'Text'
                'content'     = $mailMessage
            }
            'toRecipients' = @(
                # here you can add a loop to create a new hashtable
                # per recipient in `$toAddress`
                $Recipients | ForEach-Object {
                    @{
                        emailAddress = @{
                            address = $_
                        }
                    }
                }
            )
        }
    } | ConvertTo-Json -Depth 10
}
© www.soinside.com 2019 - 2024. All rights reserved.