Here is a set of samples I used to do a common task in my scripting life. This go around I wanted to use Powershell.
A) Create a script to send emails in powershell
—————————————————-
1) Open Powershell type set-executionpolicy Unrestricted
‘This allows scripts to be run’
‘You’ll want to also look at using RemoteSignedType
2) Type Notepad myscript.ps1
3) Paste sample code and save in myscript.ps1
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = “localhost”
$SmtpClient.host =
$SmtpServer
$From = “Friendly Reminder <[email protected]>”
$To = [email protected]
$Title = “Subject Matter”
$Body = “Body Text”
$SmtpClient.Send($from,$to,$title,$Body)
B) Testing Script
—————————————————-
1) Type ./myscript.ps1
‘Verify you receive the email.
C) How to schedule a powershell script in Windows Task Scheduler
—————————————————-
1) Create a new scheduled task.
‘The syntax is to execute the script is:
powershell -command “& ‘SomeDirmyScript.ps1′”
2) Set the script to run as your normal task scheduler accounts.
3) Execute task, verify you receive the email
Hope this helps,
Steve
I am trying to use the same command in process can you please help me out here
Dim oProcess As Process = New Process
oProcess.StartInfo.UseShellExecute = False
oProcess.StartInfo.RedirectStandardOutput = True
oProcess.StartInfo.CreateNoWindow = False
oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
oProcess.StartInfo.FileName = “powershell.exe”
Dim sArguments as string = powershell -command “& ‘SomeDirmyScript.ps1′”
oProcess.Start()
oProcess.WaitForExit(1000 * iTimeOutSec)
Are you trying to do this in a console application?
@Anil – the $To email address needs to be in quotation marks, eg:
$To = “[email protected]”
trying to send an email using powershell. I have a working code but when I substitute “From” and “To” to variables like “From=$source
“To=$destination
I get an error
working code is
$messageParameters = @{
smtpServer = “smtphost”
From = [email protected]
To = [email protected]
Subject = “You Submitted a Job”
Body = “Your Job Chain is submitted successfully”
}
Thanks for help