Powershell email

Welcome Forums General PowerShell Q&A Powershell email

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

9 months, 3 weeks ago.

  • Author
  • #101707

    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?

  • #101710

    Points: 1,184
    Helping Hand
    Rank: Community Hero

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

  • #101718

    Points: 639
    Helping Hand
    Rank: Major Contributor

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

    Get-Help Send-MailMessage -Full
            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

    Points: 303
    Helping Hand
    Rank: Contributor

    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.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort