在此方法中,我添加了一个参数 $blindcopy,我希望在调用时能够密件抄送用户。在没有添加该参数的情况下测试此脚本后,一切正常。添加此添加后,我收到一条错误消息“无法验证参数‘bcc’的参数。参数为 null 或为空”我尝试向参数添加AllowEmptyString() 属性,但仍然没有成功。非常感谢任何帮助!
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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb9e988e99ab8d8484c5888486" rel="noreferrer noopener nofollow">[email protected]</a>"
$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 "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60060f0f200201124e0f1207" rel="noreferrer noopener nofollow">[email protected]</a>" -To "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c2c1d2e0c6cfcf8ecfd2c7" rel="noreferrer noopener nofollow">[email protected]</a>" -Subject $SubjectString -Bcc $BlindCopy -Body $BodyString -BodyAsHtml -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred
}
else
{
Send-MailMessage -From "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d3dadaf5d7d4c79bdac7d2" rel="noreferrer noopener nofollow">[email protected]</a>" -To "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab8bba89abcb5b5f4b5a8bd" rel="noreferrer noopener nofollow">[email protected]</a>" -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 "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f1910103f1d1e0d51100d18" rel="noreferrer noopener nofollow">[email protected]</a>" -FileAttachment $null -BlindCopy $BccNull
最佳答案
即使函数的 -BlindCopy
参数接受空字符串,Send-MailMessage
的 -Bcc
参数仍然不接受.
我想说,对你来说最好的做法是构建参数的哈希表,添加可选参数(如果它们不为空),然后 splat 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4a1a7b1a694b2bbbbfab7bbb9" rel="noreferrer noopener nofollow">[email protected]</a>"
$pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential $user, $pass
$params = @{
'From' = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f4fdfdd2f0f3e0bcfde0f5" rel="noreferrer noopener nofollow">[email protected]</a>'
'To' = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2909380b2949d9ddc9d8095" rel="noreferrer noopener nofollow">[email protected]</a>'
'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。
关于email - 允许参数接受 Null 或空字符串 PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621323/