none
Working with multiple or single email address RRS feed

  • Question

  • I need to preface my question by saying I am a beginner when it comes to PowerShell... that said, I need help.  

    Here is what I am trying to accomplish:  

    I have a script I built that runs tasks and at the end it will email a default user specified by a parameter string. I want to give the option in this parameter to be able to change who it gets emailed to. I want to be able to email a single email address or multiple addresses. sounds easy enough until it only emails one of the emails. If not specified I see the emails working to user1, but what if you specified -MailTo "user1@company.com,user2@company.com" I have tried for several hours different ways to do this, and they have all failed. I need it to be able to have the person running the script use 1 email address (no array needed) or multiple addresses (array needed). 

    Param(
        [string]$MailTo="user1@company.com"
    )
    
    Send-MailMessage -smtpServer "smtp.company.com" -to $MailTo -From "from@company.com" -subject "Hello!" -Body "Hi there!"



    -AAP (Andrew)




    • Edited by apetheo Monday, June 17, 2019 8:32 PM
    Monday, June 17, 2019 8:18 PM

Answers

  • Are you writing a function?  Multiple addresses would be a string array [string[]], like 'user1@company.com','user2@company.com'.  With a cmdlet or function, you can drop the quotes.

    • Edited by JS2010 Monday, June 17, 2019 8:38 PM
    • Proposed as answer by jrvModerator Monday, June 17, 2019 8:46 PM
    • Marked as answer by apetheo Monday, July 8, 2019 9:26 PM
    Monday, June 17, 2019 8:37 PM

All replies

  • Are you writing a function?  Multiple addresses would be a string array [string[]], like 'user1@company.com','user2@company.com'.  With a cmdlet or function, you can drop the quotes.

    • Edited by JS2010 Monday, June 17, 2019 8:38 PM
    • Proposed as answer by jrvModerator Monday, June 17, 2019 8:46 PM
    • Marked as answer by apetheo Monday, July 8, 2019 9:26 PM
    Monday, June 17, 2019 8:37 PM
  • Thank you! That was it!

    -AAP (Andrew)

    Monday, July 8, 2019 9:26 PM