我定义了多个具有不同正文内容的变量,稍后可以在带有-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中获得正确的返回类型? 预先非常感谢。
答案 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
将$var
和Get-Variable
中的变量名混合在一起
If ($var -in 'it','de','mx'){
$Body = (get-variable -name "body$var").Value
} else {
$Body = $bodye
}