Email 允许param接受Null或空字符串PowerShell
在这个方法中,我添加了一个参数$blindcopy,我希望在调用该参数时能够将其发送给bcc用户。在没有添加参数的情况下测试该脚本之后,一切正常。添加此项后,我收到一个错误,提示“无法验证参数“bcc”上的参数。该参数为null或空”。我已尝试向参数添加AllowEmptyString()属性,但仍然没有成功。非常感谢您的帮助Email 允许param接受Null或空字符串PowerShell,email,powershell,bcc,Email,Powershell,Bcc,在这个方法中,我添加了一个参数$blindcopy,我希望在调用该参数时能够将其发送给bcc用户。在没有添加参数的情况下测试该脚本之后,一切正常。添加此项后,我收到一个错误,提示“无法验证参数“bcc”上的参数。该参数为null或空”。我已尝试向参数添加AllowEmptyString()属性,但仍然没有成功。非常感谢您的帮助 cls $BccNull = "" function SendEmail([string]$BodyString,[string]$SubjectString,[st
cls
$BccNull = ""
function SendEmail([string]$BodyString,[string]$SubjectString,[string[]]$EmailRecipientsArray,[string]$FileAttachment=$null,[AllowEmptyString()][string]$BlindCopy)
{
$MethodName = "Send Email"
# Send the HTML Based Email using the information from the tables I have gathered information in
try
{
$user = "user@foo.com"
$pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $user, $pass
$SMTPServer = "some.mail.server"
if([string]::IsNullOrEmpty($BodyString))
{
$BodyString = " Body text was empty for user: $ErrorMessageUserName"
}
if([string]::IsNullOrEmpty($FileAttachment))
{
Send-MailMessage -From "foo@bar.org" -To "bar@foo.org" -Subject $SubjectString -Bcc $BlindCopy -Body $BodyString -BodyAsHtml -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred
}
else
{
Send-MailMessage -From "foo@bar.org" -To "bar@foo.org" -Subject $SubjectString -Body $BodyString -BodyAsHtml -Attachments $FileAttachment -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred
}
}
catch
{
Write-Host "An Exception has occurred:" -ForegroundColor Red
Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
#$ErrorMessage = "Script Error: "+ $_.Exception.Message + "`n" + "Exception Type: $($_.Exception.GetType().FullName)"
#$SubjectLine = "Script Error: " + $MethodName + " " + $ErrorMessageUserName
#SendEmail -BodyString $ErrorMessage -SubjectString $SubjectLine -EmailRecipientsArray $EmailErrorAddress -FileAttachment $null
$SuccessfulRun = $false
#ReturnStatusError -CurrentStatus "Error" -ErrorMessage $_.Exception.Message
}
}
SendEmail -BodyString "Test" -SubjectString "Test" -EmailRecipientArray "foo@bar.org" -FileAttachment $null -BlindCopy $BccNull
即使函数的
-BlindCopy
参数接受空字符串,发送邮件的-Bcc
参数仍然不接受空字符串
我认为对您来说,最好的做法是构造参数的哈希表,如果可选参数不是空的,则添加可选参数,然后在cmdlet上创建哈希表
function Send-Email {
Param(
[Parameter(Mandatory=$true)]
[string]$BodyString,
[Parameter(Mandatory=$true)]
[string]$SubjectString,
[Parameter(Mandatory=$true)]
[string[]]$EmailRecipientsArray,
[Parameter(Mandatory=$false)]
[string]$FileAttachment = '',
[Parameter(Mandatory=$false)]
[AllowEmptyString()]
[string]$BlindCopy = ''
)
try {
$user = "user@foo.com"
$pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential $user, $pass
$params = @{
'From' = 'foo@bar.org'
'To' = 'bar@foo.org'
'Subject' = $SubjectString
'Body' = $BodyString
'Priority' = 'High'
'dno' = 'onSuccess', 'onFailure'
'SmtpServer' = 'some.mail.server'
'Credential' = $cred
}
if ($BlindCopy) { $params['Bcc'] = $BlindCopy }
if($FileAttachment) { $params['Attachments'] = $FileAttachment }
Send-MailMessage @params -BodyAsHtml
} catch {
...
}
}
但是即使使用splatting,我可能仍然不允许参数-BlindCopy
使用空字符串。如果消息不应以密件传给某人,则应完全忽略该参数。附件也是如此。如果空字符串显示为密件抄送收件人(或附件),函数应抛出错误。依我拙见YMMV