2014-04-01 23 views
1

我已經搜索了很多很多論壇,他們解釋瞭如何做到這一點,但技術語言太難以理解,因爲我對PowerShell非常陌生。我想這一步一步地向我解釋(嬰兒的步驟)。我想在批處理文件(.bat)中運行這個powershell命令。我有一個每週執行robocopy備份的批處理文件,我希望批處理文件在備份完成時向我發送電子郵件。我唯一的問題是憑據,我得到一個彈出框詢問用戶名和密碼。當我發送這些信息時,電子郵件將成功發送。這是我的;使用send-mailmessage命令時的電子郵件憑證

使用:PowerShell的V2.0 Windows 7旗艦版

Powershell -command send-mailmessage -to [email protected] -from [email protected] -smtp smtp.broadband.provider.com -usessl -subject 'backup complete' 

回答

2
$from = "[email protected]" 
$to = "[email protected]" 
$smtp = "smtpAddress.com" 
$sub = "hi" 
$body = "test mail" 
$secpasswd = ConvertTo-SecureString "yourpassword" -AsPlainText -Force 
$mycreds = New-Object System.Management.Automation.PSCredential($from, $secpasswd) 
Send-MailMessage -To $to -From $from -Subject $sub -Body $body -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml 
+0

這在Win10上通過gmail SMTP工作。 –

2

你可以通過憑證對象在同一命令 - 這將避免彈出:

Powershell -Command 'Send-MailMessage -to "[email protected]" -from "[email protected]" -smtp "smtp.broadband.provider.com" -usessl -subject "backup complete" -credential (new-object System.Net.NetworkCredential("user","pass","domain"))' 

我建議以更安全的格式存儲用戶名/密碼,但這應該做你的伎倆。

相關問題