首页 > 解决方案 > 从 (Send-MailMessage) 返回到 -body 参数中的语言特定变量

问题描述

我已经定义了几个具有不同正文内容的变量,稍后我可以在 Send-MailMessage 命令中使用 -Body 参数引用这些变量。

$bodyit = @"
Hello Italy
"@

$bodyde = @"
Hello Germany
"@

$bodymx = @"
Hello Mexico
"@

$bodye = @"
Hello International
"@

if ($var -eq "IT"){
return $bodyit
}elseif ($var -eq "DE"){
return $bodyde
}elseif ($var -eq "MX"){
return $bodymx
}else {return $bodye}

Send-MailMessage -from "..." -to "..." -subject "..." -smtp "..." -Body $return to specific $body

如何在 Send-MailMessage 中获得正确的返回类型?提前谢谢了。

标签: powershellreturn-type

解决方案


尝试使用开关而不是多个 if,还将您实际想要的值存储到$body$body用于Send-Mail正确响应。

试试这样:

switch ($var)
{
  IT {
    $body = @"
    Hello Italy
    "@
  }
  DE {
    $body = @"
    Hello Germany
    "@
  }
  MX {
    $body = @"
    Hello Mexico
    "@
  }
  default {
    $body = @"
    Hello International
    "@
  }
}

Send-MailMessage -from "..." -to "..." -subject "..." -smtp "..." -Body $body

推荐阅读