从(Send-MailMessage)返回-body参数中特定于语言的变量

时间:2018-07-02 14:11:23

标签: powershell return-type

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

$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中获得正确的返回类型? 预先非常感谢。

2 个答案:

答案 0 :(得分:2)

尝试使用开关而不是多个开关,还将实际需要的值存储到$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

答案 1 :(得分:1)

您可以按照以下方法使用cmdlet body$varGet-Variable中的变量名混合在一起

If ($var -in 'it','de','mx'){
    $Body = (get-variable -name "body$var").Value
} else {
    $Body = $bodye
}