2013-09-26 80 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'