LoginSignup
2
1

More than 5 years have passed since last update.

Azure Automationで複数アドレスにメール送信する

Posted at

Azure Automationの中で、Send-MailMessageコマンドを使ってメール送信する際、複数の宛先に送るやり方です。

PowerShellの配列でやってみる

  • PowerShellで配列を使う場合以下のような書式になります。
[System.String[]] $MailTo = @("mail@example.com","mail2@example.com")
  • こんな感じでAutomationの中でべた書きすればうまくいきます。(MailTo以外の変数宣言は省略してます)
[System.String[]] $MailTo = @("mail@example.com","mail2@example.com")
Send-MailMessage -UseSsl -From $MailFrom -To $MailTo -Subject $Subject -Body $Body -SmtpServer $SmtpServer -Port $Port -Credential $cred
  • でも、Automation自体は固定にして、送り先メールアドレスはパラメータで渡してあげたい場合って多くあると思います。
  • その場合に、このようなAutomationにして、パラメータを以下のように渡した場合、
Param(
    [parameter(Mandatory=$True)]
    [System.String[]] $MailTo
)
Send-MailMessage -UseSsl -From $MailFrom -To $MailTo -Subject $Subject -Body $Body -SmtpServer $SmtpServer -Port $Port -Credential $cred
  • エラーメッセージを吐いてコケます。Automationが配列として認識できてないようです。
Unable to cast object of type 'System.String' to type 'System.Object[]'.

JSON形式で渡す

Param(
    [parameter(Mandatory=$True)]
    [System.String[]] $MailTo
)
Send-MailMessage -UseSsl -From $MailFrom -To $MailTo -Subject $Subject -Body $Body -SmtpServer $SmtpServer -Port $Port -Credential $cred
  • 気を付けないといけないのが、単一のメールアドレスに送る場合もJSONで渡す必要が出てきます。
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1