简体   繁体   English

Powershell:如何将空体传递给Send-MailMessage?

[英]Powershell: How to pass empty body to Send-MailMessage?

I have this simple script: 我有这个简单的脚本:

param([string[]]$to="markk", $subject=$null, $body=$null, $from="name", $suffix="@example.com", $server="dev-builder")

function NormalizeAddress([string]$address)
{
  if ($address -notmatch "@")
  {
    $address = $address + $suffix
  }
  $address
}

if (! $subject)
{
  $subject = [DateTime]::Now
}

$from = NormalizeAddress $from
$to = $to | % { NormalizeAddress $_ }
Send-MailMessage -To $to -Subject $subject -From $from -Body $body  -SmtpServer $server

It is written that way so that one could run it without any arguments, in which case a test message would be sent to the author (me). 它以这样的方式编写,以便可以在没有任何参数的情况下运行它,在这种情况下,测试消息将被发送给作者(我)。

Currently the script fails, because passing $null in the -Body argument is not allowed: 目前脚本失败,因为不允许在-Body参数中传递$null

Send-MailMessage : Impossible de valider l'argument sur le paramètre « Body ». L'argument est null ou vide. Indiquez un argument qui n'est pas null ou vide et réessayez.
Au niveau de C:\Work\hg\utils\SendEmail.ps1 : 19 Caractère : 61
+ Send-MailMessage -To $to -Subject $subject -From $from -Body <<<<  $body  -SmtpServer $server
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

I have three possible solutions: 我有三种可能的解决方案:

  • Two Send-MailMessage command statements: 两个Send-MailMessage命令语句:

if ($body)
{
  Send-MailMessage -To $to -Subject $subject -From $from -Body $body -SmtpServer $server
}
else
{
  Send-MailMessage -To $to -Subject $subject -From $from -SmtpServer $server
}

  • Using Invoke-Expression : 使用Invoke-Expression

$expr = "Send-MailMessage -To `"$to`" -Subject `"$subject`" -From `"$from`" -SmtpServer `"$server`""
if ($body)
{
  $expr = "$expr -Body `"$body`""
}

Invoke-Expression $expr

  • Simulating an empty body with a single space character: 使用单个空格字符模拟空体:

if (! $body)
{
  $body = " "
}
Send-MailMessage -To $to -Subject $subject -From $from -Body $body -SmtpServer $server

All of these solutions look bad to me, because they all have this smell of being just hacks. 所有这些解决方案对我来说都很糟糕,因为它们都有这种气味。 I must be missing something really basic here, so my question is how can I pass the empty body without having to resort to these hacks? 我必须在这里遗漏一些非常基本的东西,所以我的问题是如何在不必诉诸这些黑客的情况下通过空体?

Thanks. 谢谢。

The Send-MailMessage cmdlet can send emails without specifying the Body parameter. Send-MailMessage cmdlet可以在不指定Body参数的情况下发送电子邮件。 On the other hand, if you do specify a value it must not be empty or null (there's a validation attribute on the parameter, ValidateNotNullOrEmpty). 另一方面,如果确实指定了值,则它不能为空或为null(参数上有验证属性,ValidateNotNullOrEmpty)。 So, the solution would be to create hashtable of parameter names and values (aka splatting) and pass it to the underlying cmdlet: 因此,解决方案是创建参数名称和值的哈希表(也称为splatting)并将其传递给底层cmdlet:

 param([string[]]$to="markk", $subject=$null, $body=$null, $from="name", $suffix="@example.com", $server="dev-builder")

 function NormalizeAddress([string]$address)
 {
   if ($address -notmatch "@")
   {
     $address = $address + $suffix
   }
   $address
 }

 if (! $subject)
 {
   $subject = [DateTime]::Now
 }

 $param = @{
    from=NormalizeAddress $from
    to = $to | foreach { NormalizeAddress $_ }
    subject = $subject
    smtpserver = $server    
 }

 # if body is not empty or null - add it to the hastable
 if( ![String]::IsNullOrEmpty($body) ) {$param['body']=$body}

 Send-MailMessage @param

You can use a "splat" variable to pass parameters indirectly: 您可以使用“splat”变量间接传递参数:

$extraParams = @{}
if (-not [String]::IsNullOrEmpty($body)) {
  $extraParams['Body'] = $body
}

Send-MailMessage -To $to -Subject $subject -From $from -SmtpServer $server @extraParams

Note use of @ to pass hash table of parameter-name parameter-value pairs (you can of course pass all the parameters this way, eg. in the initialisation of $extraParams ). 注意使用@来传递参数名称参数 - 值对的哈希表(当然,您可以通过这种方式传递所有参数,例如在$extraParams的初始化中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM