Why does this function not work?

Author Message
marcussmith

  • Total Posts : 7
  • Scores: 0
  • Reward points : 0
  • Joined: 1/30/2006
  • Status: offline
Why does this function not work? Friday, December 07, 2007 12:38 AM (permalink)
0
I have tried chaning this email code to a function but after running the script then trying to use the function " smtp-mail "email.address@domain.com" but get the error "is not recognized as a cmdlet, function, operable program"

function smtp-mail ([string]$to ="")

{

$SmtpClient = new-object system.net.mail.smtpClient

$MailMessage = New-Object system.net.mail.mailmessage

$SmtpClient.Host = "relay"

$mailmessage.from = ("test.email@domain.com ")

$mailmessage.To.add($to)

$mailmessage.Subject = "Test Email”

$mailmessage.Body = "Testing”

$smtpclient.Send($mailmessage)

}
<message edited by marcussmith on Friday, December 07, 2007 1:28 AM>
 
#1
    SAPIENScripter

    • Total Posts : 283
    • Scores: 2
    • Reward points : 0
    • Joined: 11/1/2006
    • Location: SAPIEN Technologies
    • Status: offline
    RE: Why does this function not work? Friday, December 07, 2007 1:57 AM (permalink)
    0
    Is this function in your profile or did you paste it into your Powershell session?  The function worked fine for me.  If you are using it in a script, it has be inserted at the beginning of your script before any other code that uses it.  Unlike VBScript where functions can be any where, functions in a PowerShell script must be defined at the beginning of the script before you can use it.  Also, if the function is in a script,  it only "exists" in the script's scope meaning once the script ends it is not available in the general console session.  If you want to load the function into your console when running the script, then you need to dot source it:

    PS C:\. ./myscript.ps1

    The script, myscript.ps1, will execute and any functions will remain in the global scope, ie your session.
    Jeffery Hicks
    Windows PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

    Follow Me: http://www.twitter.com/JeffHicks
     
    #2
      marcussmith

      • Total Posts : 7
      • Scores: 0
      • Reward points : 0
      • Joined: 1/30/2006
      • Status: offline
      RE: Why does this function not work? Sunday, December 09, 2007 9:50 PM (permalink)
      0
      Hi
       
      After following your recommendation the function worked.
       
      Regards
      Marcus
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9