我已经完成了我想在Powershell
发送邮件的命令。这是我的代码
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'
当我在命令提示符下执行时,上面的代码工作正常,但是当我尝试创建.bat
文件时没有。我用代码做了什么问题?
答案 0 :(得分:4)
从文件中删除powershell.exe并将其另存为.ps1然后创建一个.bat文件并编写powershell.exe -file myscript.ps1
Bat文件:
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'