none
Powershell function parameter question: "The argument is null or empty" RRS feed

  • Question

  • Hello guys,

    I'm doing my first steps with Powershell. I think I will need some time to get used to it.

    What I'm trying to achieve is simple: send an email with a variable "cc" address which may be empty:

    $from = "sender@domain.com"
    $to = "receiver@domain.com"
    $cc =""
    
    Send-MailMessage -from $from -to $to -cc $cc

    This gives me the following error message: 

    Cannot validate argument on parameter 'Cc'. The argument is null or empty.

    So how do I solve this issue? Of course I could do

    if( [string]::IsNullOrEmpty($cc) ) {
        Send-MailMessage -from $from -to $to
    }

    But this is no real solution because maybe I also have a $bcc and an an $attachment argument that may or may not be empty. So I would have to write three nested if-else blocks - a maintainability hell.

    So again - what would I do to solve this? Strangely I couldn't find an answer to this question - or even the question itself - on google, though I consider it very obvious.

    Thanks,

    Carsten

    Monday, February 15, 2016 4:20 PM

Answers

  • Hi,

    Personally I'd create a hash with your minimum requirements (for me that's To, From, Subject, Body, and SmtpServer) and then test for additional requirements. If there's a CC, add CC to the hash. So and and so forth until you build your entire hashtable. Then splat to send with Send-MailMessage.

    http://ss64.com/ps/syntax-hash-tables.html

    Example:

    $mailProps = @{
        To = 'to@domain.com'
        From = 'donotreply@domain.com'
        Subject = 'subjectLine'
        Body = 'Body Text'
        SmtpServer = 'smtp.domain.com'
    }
    
    $cc = 'cc1@domain.com','cc2@domain.com'
    
    If ($cc) {
    
        $mailProps.Add('Cc',$cc)
    
    }
    
    If ($bcc) {
    
        $mailProps.Add('Bcc',$bcc)
    
    }
    
    If ($attachmentments) {
    
        $mailProps.Add('Attachments',$attachmentments)
    
    }
    
    Send-MailMessage @mailProps


    • Marked as answer by carstensfp Monday, February 15, 2016 4:57 PM
    Monday, February 15, 2016 4:40 PM

All replies

  • use a splat array
    Monday, February 15, 2016 4:32 PM
  • You cannot use null or blank parameters in PowerShell.

    \_(ツ)_/

    Monday, February 15, 2016 4:32 PM
  • Hi,

    Personally I'd create a hash with your minimum requirements (for me that's To, From, Subject, Body, and SmtpServer) and then test for additional requirements. If there's a CC, add CC to the hash. So and and so forth until you build your entire hashtable. Then splat to send with Send-MailMessage.

    http://ss64.com/ps/syntax-hash-tables.html

    Example:

    $mailProps = @{
        To = 'to@domain.com'
        From = 'donotreply@domain.com'
        Subject = 'subjectLine'
        Body = 'Body Text'
        SmtpServer = 'smtp.domain.com'
    }
    
    $cc = 'cc1@domain.com','cc2@domain.com'
    
    If ($cc) {
    
        $mailProps.Add('Cc',$cc)
    
    }
    
    If ($bcc) {
    
        $mailProps.Add('Bcc',$bcc)
    
    }
    
    If ($attachmentments) {
    
        $mailProps.Add('Attachments',$attachmentments)
    
    }
    
    Send-MailMessage @mailProps


    • Marked as answer by carstensfp Monday, February 15, 2016 4:57 PM
    Monday, February 15, 2016 4:40 PM
  • Thanks,

    that's not perfect but much better than the "if"-solution.

    Carsten


    Monday, February 15, 2016 4:57 PM