2

In this method, I have added a parameter $blindcopy that I want to be able to bcc users when called. After testing this script without that parameter added, all is well. After adding this addition, I receive an error saying "Cannot validate the argument on parameter 'bcc'. The argument is null or empty" I have tried adding AllowEmptyString() attribute to the parameter but still no luck. Any help is much appreciated!

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
johnnyjohnson
  • 93
  • 3
  • 11

1 Answers1

2

Even if the -BlindCopy parameter of your function accepts an empty string, the -Bcc parameter of Send-MailMessage still doesn't.

I'd say the best course of action for you would be to construct a hashtable of your parameters, add optional parameters if they're not empty, and then splat the hashtable on the 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 {
        ...
    }
}

But even with splatting I probably still wouldn't allow empty strings for the parameter -BlindCopy. If the message isn't supposed to be BCC'd to someone that parameter should be omitted entirely. The same goes for attachments. If an empty string shows up as a BCC recipient (or attachment), the function should throw an error. IMHO. YMMV.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328