使用可能指定或未指定的参数运行内置cmdlet

时间:2013-10-28 16:11:13

标签: powershell powershell-v2.0

我的脚本中有一个函数包含send-mailmessage cmdlet,并在发送每封电子邮件之前做了一些逻辑。

我的问题是如何创建参数列表以输入send-mailmessage,以便Cc,Bcc和附件是可选的。

之前我做过的只是一些if语句。但是在混合中添加-Cc,如果是语句而不是当前的4,那将是9个。我想我已经过度思考了这个并且必须有一个更简单的方法。

剥离功能:

Function Send-Email ($To, $Cc, $Bcc, $Subject, $From, $Body, $Attachments)
{
    #Some more code goes here that sometimes modifies the incoming parameters
    $EmailServer = my.mail.server
    Try
    {
        #Need to add ability for $Cc to be optional
        if ($Attachments -and $Bcc) {send-mailmessage -to $To -Cc $Cc -Bcc $Bcc -subject $Subject -From $From -body $Body -smtpserver $EmailServer -attachments $Attachments -ErrorAction "Stop"}
        if ($Attachments -and -not $Bcc) {send-mailmessage -to $To -Cc $Cc -subject $Subject -From $From -body $Body -smtpserver $EmailServer -attachments $Attachments -ErrorAction "Stop"}
        if ($Bcc -and -not $Attachments) {send-mailmessage -to $To -Cc $Cc -Bcc $Bcc -subject $Subject -From $From -body $Body -smtpserver $EmailServer -ErrorAction "Stop"}
        if (-not $Attachments -and -not $Bcc) {send-mailmessage -to $To -Cc $Cc -subject $Subject -From $From -body $Body -smtpserver $EmailServer -ErrorAction "Stop"}
    }
    Catch [system.exception]
    {
        "Failed to send email to $($To) due to: $_"  #Logging removed for SO example code
    }
    Finally
    {}
}

我已经尝试将参数生成为一个长字符串或者也可以作为字符串数组,然后使用invoke-expression。我也试过了,但是使用'&'用于执行cmdlet的标志。

当我尝试时,我刚刚收到shell的参数提示。

PS E:\script> invoke-expression 'send-mailmessage $a'

cmdlet Send-MailMessage at command pipeline position 1
Supply values for the following parameters:
From: 


PS E:\script> & send-mailmessage $a

cmdlet Send-MailMessage at command pipeline position 1
Supply values for the following parameters:
From: 

我已经尝试将$ Cc设置为$ Null,或$ False或空字符串“”,但send-mailmessage会抱怨参数无效。

我觉得我在这里缺少一些简单的东西,但在我看来,这个函数应该看起来像这个无效的例子:

Function Send-Email ($To, $Cc, $Bcc, $Subject, $From, $Body, $Attachments)
{
    #Some more code goes here that sometimes modifies the incoming parameters
    $EmailServer = my.mail.server
    if ($Cc) {$CcArg = "-Cc $Cc"}
    if ($Bcc) {$BccArg = "-Bcc $Bcc"}
    if ($Attachments) {$AttachArg = "-Attachments $Attachments"}
    Try
    {
        send-mailmessage -to $To ($CcArg) ($BccArg) -subject $Subject -From $From -body $Body -smtpserver $EmailServer $AttachArg -ErrorAction "Stop"
    }
    Catch [system.exception]
    {
        "Failed to send email to $($To) due to: $_"  #Logging removed for SO example code
    }
    Finally
    {}
}

感谢您的任何想法或建议。

2 个答案:

答案 0 :(得分:2)

啊,哈哈! 找到这篇文章:Inline expansion of powershell variable as cmdlet parameter?

所以函数看起来像(未完全测试,但原理有效):

Function Send-Email ($To, $Cc, $Bcc, $Subject, $From, $Body, $Attachments)
{
    #Some more code goes here that sometimes modifies the incoming parameters
    $EmailServer = my.mail.server
    $MoreArgs = @{}
    if ($Cc) {$MoreArgs.Add("Cc",$Cc)}
    if ($Bcc) {$MoreArgs.Add("Bcc",$Bcc)}
    if ($Attachments) {$MoreArgs.Add("Attachments",$Attachments)}
    Try
    {
        send-mailmessage -to $To -subject $Subject -From $From -body $Body -smtpserver $EmailServer -ErrorAction "Stop" @MoreArgs
    }
    Catch [system.exception]
    {
        "Failed to send email to $($To) due to: $_"  #Logging removed for SO example code
    }
    Finally
    {}
}

答案 1 :(得分:2)

另一个消除所有Ifs的选项。

$EmailParams = @{
 To          = $To
 Cc          = $Cc
 From        = $From
 Subject     = $Subject
 Body        = $body
 SMTPServer  = $MailServer
 Attachments = $Attachments
 ErrorAction = 'Stop'
 }

 $EmailParams.keys |
  Where {$EmailParams.$_ -eq $null } |
  foreach { $EmailParams.remove($_) }

 Try { Send-MailMessage @EmailParams }
 Catch { "Failed to send email to $($To) due to: $_"  }
 Finally {}