Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Why does this function not work?

 
Logged in as: Guest
arrSession:exec spGetSession 2,5,54081
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Windows PowerShell >> Why does this function not work?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Why does this function not work? - 12/6/2007 11:38:44 PM   
  marcussmith

 

Posts: 5
Score: 0
Joined: 1/30/2006
Status: offline
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 -- 12/7/2007 12:28:16 AM >
 
 
Post #: 1
 
 RE: Why does this function not work? - 12/7/2007 12:57:15 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
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

(in reply to marcussmith)
 
 
Post #: 2
 
 RE: Why does this function not work? - 12/9/2007 8:50:00 PM   
  marcussmith

 

Posts: 5
Score: 0
Joined: 1/30/2006
Status: offline
Hi

After following your recommendation the function worked.

Regards
Marcus

(in reply to SAPIENScripter)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Windows PowerShell >> Why does this function not work? Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts