使用powershell脚本制作批处理文件

时间:2013-09-26 10:29:19

标签: powershell batch-file smtp

我已经完成了我想在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文件时没有。我用代码做了什么问题?

1 个答案:

答案 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'