Powershell email

Welcome Forums General PowerShell Q&A Powershell email

This topic contains 3 replies, has 4 voices, and was last updated by

 
Participant
6 months, 2 weeks ago.

  • Author
    Posts
  • #101707
    SK

    Participant
    Points: 0
    Rank: Member

    $File="gci \\abcd\efgh\ijkl | select -last 1"
    $From = "adcb@gmail.com"
    $To = "efgh@gmail.com"
    $Attachment = $File
    $Subject = "Here's the Email Subject"
    $Body = "This is what I want to say"
    $SMTPServer = "smtp.gmail.com"
    Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment

    This is the error I receive:
    Send-MailMessage : Illegal characters in path.
    At line:1 char:1
    + Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Send-MailMessage], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.SendMailMessage

    What am I doing wrong here?
    Thanks

  • #101710

    Participant
    Points: 269
    Helping Hand
    Rank: Contributor

    What's in your variable $Attachment? How does it look?

  • #101718

    Participant
    Points: 342
    Helping Hand
    Rank: Contributor

    Leverage Get-Help to understand what is expected for the parameter:

    Get-Help Send-MailMessage -Full
    
    PARAMETERS
        -Attachments 
            Specifies the path and file names of files to be attached to the email message. You can use this parameter or pipe the paths and file names to Send-MailMessage .
            
            Required?                    false
            Position?                    named
            Default value                None
            Accept pipeline input?       True (ByValue)
            Accept wildcard characters?  false
    

    The String[] indicates a string array, specifically an array of paths. If you run your command:

    PS C:\Users\Rob> Get-ChildItem -Path C:\Scripts | Select -Last 1
    
    
        Directory: C:\Scripts
    
    
    Mode                LastWriteTime         Length Name                                                                                                                                             
    ----                -------------         ------ ----                                                                                                                                             
    -a----        5/28/2018   1:41 PM            202 test.csv                                                                                                                                         
    

    It is returning an object, so you need to get a Property of that object, which is FullName. Additionally, if you look at about_Splatting, you can make the code much cleaner:

    $File = (Get-ChildItem -Path C:\Scripts | Select -ExpandProperty FullName -Last 1)
    
    $mailParams = @{
        From       = "adcb@gmail.com"
        To         = "efgh@gmail.com"
        Attachment = $File
        Subject    = "Here's the Email Subject"
        Body       = "This is what I want to say"
        SMTPServer = "smtp.gmail.com"
    }
    
    Send-MailMessage @mailParams
    
  • #101721

    Participant
    Points: 112
    Helping Hand
    Rank: Participant

    I believe the issue is with your file variable. You have double quotes around the entire command. Use the -LiteralPath parameter with double quotes around the value if necessary. As it is now, $file is nothing more than a PowerShell command saved as a string. I think the -attatchment parameter probably wants a full path to a file? Not in front of a computer right now so can't confirm. But your file variable stood out to me as a likely issue.

The topic ‘Powershell email’ is closed to new replies.