2013-09-26 78 views
2

我已经完成了我想用Powershell发送邮件的命令。这是我的代码使用powershell脚本创建批处理文件

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' 

上面的代码工作正常,当我在命令提示符下执行,而不是当我尝试做一个.bat文件。我用代码做了什么问题?从文件

+0

你得到什么错误? –

+0

当我尝试执行'.bat'文件时,命令提示符打开并执行'poweshell.exe'并停止,像这样的'I:\> powershell.exe Windows PowerShell 版权所有(C)2012 Microsoft Corporation。版权所有。 PS I:\>' – niren

+2

尝试从文件中删除powershell.exe并将其保存为.ps1,然后创建一个.bat文件并写入powershell.exe-文件myscript.ps1 –

回答

4

删除powershell.exe并保存为名为.ps1然后创建一个.bat文件,写powershell.exe -file myscript.ps1

.bat文件:

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'