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:
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:
Send-MailMessage
command statements: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
}
Invoke-Expression
:$expr = "Send-MailMessage -To `"$to`" -Subject `"$subject`" -From `"$from`" -SmtpServer `"$server`""
if ($body)
{
$expr = "$expr -Body `"$body`""
}
Invoke-Expression $expr
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. 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). So, the solution would be to create hashtable of parameter names and values (aka splatting) and pass it to the underlying 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With