niren
niren

Reputation: 2741

Make batch file with powershell script

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?

Upvotes: 2

Views: 11112

Answers (1)

Musaab Al-Okaidi
Musaab Al-Okaidi

Reputation: 3784

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'

Upvotes: 6

Related Questions