I have done the commands that I want to send mail in Powershell
. This is my code
powershell.exe
$user="username@gamil.com"
$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 'username@gamil.com' -To 'usernametwo@gamil.com' -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?
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="username@gamil.com"
$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 'username@gamil.com' -To 'usernametwo@gamil.com' -Subject 'failure Test'
来源:https://stackoverflow.com/questions/19025741/make-batch-file-with-powershell-script