2

I have done the commands that I want to send mail in Powershell. This is my code

powershell.exe
$user="[email protected]"
$pass=cat I:\password.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass
send-MailMessage -SmtpServer smtp.gmail.com -Credential $mycred -Usessl true -From '[email protected]' -To '[email protected]' -Subject 'failure Test'

Above code is working fine when I execute in command prompt, but not When I try to make a .bat file. What is the problem I have done with code?

4
  • What's the error that you get? Sep 26, 2013 at 10:31
  • When I try to execute .bat file, the commmand prompt open and execute poweshell.exe and stops, like this I:\>powershell.exe Windows PowerShell Copyright (C) 2012 Microsoft Corporation. All rights reserved. PS I:\>
    – niren
    Sep 26, 2013 at 10:33
  • 2
    try removing powershell.exe from the file and save it as .ps1 then create a .bat file and write powershell.exe -file myscript.ps1 Sep 26, 2013 at 10:34
  • Cheers. I'll post it below so you can select it as the correct answer. Thanks Sep 26, 2013 at 10:40

1 Answer 1

6

Remove powershell.exe from the file and save it as .ps1 then create a .bat file and write powershell.exe -file myscript.ps1

Bat file:

powershell.exe -file myscript.ps1

myScript.ps1:

$user="[email protected]"
$pass=cat I:\password.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass
send-MailMessage -SmtpServer smtp.gmail.com -Credential $mycred -Usessl true -From '[email protected]' -To '[email protected]' -Subject 'failure Test'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.