gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:52:32 24 4
gpt4 key购买 nike

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

我的问题是如何创建参数列表以输入发送邮件消息,以便抄送、密送和附件是可选的。

我之前所做的只是一些 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
{}
}

我尝试将参数生成为一个长字符串或字符串数​​组,然后使用调用表达式。我也尝试过,但使用“&”符号来执行 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
{}
}

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

最佳答案

啊哈哈!
找到这篇文章: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
{}
}

关于powershell - 使用可能指定或未指定的参数运行内置 cmdlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19639763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com