使用可以指定也可以不指定的参数运行内置cmdlet [英] Run built-in cmdlet with parameters that may or may not be specified

查看:17
本文介绍了使用可以指定也可以不指定的参数运行内置cmdlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本中有一个函数,它包装Send-mailMessage cmdlet,并在发送每封电子邮件之前执行一些逻辑操作。

我的问题是如何创建参数列表以馈送到Send-mailMessage中,以便抄送、密件抄送和附件是可选的。

我以前所做的只是几个if语句。但将-cc添加到混合中,将是9个if语句,而不是目前的4个。我想我想得太多了,应该有一个更简单的方法。

精简函数:

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。

当我尝试时,外壳程序只是提示我输入参数。

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
    {}
}

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

推荐答案

啊哈! 找到此帖子: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
    {}
}

这篇关于使用可以指定也可以不指定的参数运行内置cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆