Send-Mailmessage超时

时间:2018-03-01 09:57:25

标签: powershell

我希望改进一个旧的批处理脚本并将其升级到powershell,这是一个robocopy批处理脚本,我希望它能够在完成后发送一个带有日志文件的邮件。我设法得到驱动器映射和robocopy部分排序但我有一些问题让send-mailmessage部分工作

`$SourceFolder = "V:\"
$DestinationFolder = "Y:\"
$Dateandtime = Get-Date -format filedate
$password = XXXXXXXXX
$Subject = "Robocopy Results: Backup USA - NL"
$SMTPServer = "mailserver.domain.com"
$Sender = "backupusr@domain.com"
$Username = "backupusr"
$Recipients = "administrator@domain.com"
$Admin = "administrator@domain.com"
$SendEmail = $True
$IncludeAdmin = $True
$AsAttachment = $False
$Cred =  new-object Management.Automation.PSCredential $Username, ($password 
| ConvertTo-SecureString -AsPlainText -Force)`

这是导致脚本超时的行

Send-MailMessage -SmtpServer $SMTPServer -From $Sender -To $Recipients -Subject $Subject -Body "Robocopy results are attached." -DeliveryNotificationOption onFailure -UseSsl -Credential $Cred

这是我收到的错误

  

Send-MailMessage:操作已超时。   在C:\ Scripts \ bpcti-robocopy.ps1:113 char:1   + Send-MailMessage -SmtpServer $ SMTPServer -From $ Sender -To $ Recipient ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~       + CategoryInfo:InvalidOperation:(System.Net.Mail.SmtpClient:SmtpClient)[Send-MailMessage],SmtpException       + FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

-Credential参数接受一个PSCredential对象。您可以使用Get-Credential使用用户名和密码组合来交互式地创建一个。更详细的here

或者,由于您的脚本中密码已经是明文形式,因此您可以按照here所述使用该过程并构造一个新的PSCredential

# Convert the plaintext password into a secure string.
$SecurePassword = ConvertTo-SecureString "XXXXXXXXX" -AsPlainText -Force
# Create a new PSCredential using the username and secure string password.
$Cred = New-Object System.Management.Automation.PSCredential ("backupusr@domain.com", $SecurePassword)

Send-MailMessage -SmtpServer mailserver.domain.com -From backupusr@domain.com -To administrator@domain.com -Subject testing -Body "Robocopy results are attached." -DeliveryNotificationOption onFailure -Credential $Cred

请注意,用户名应包含@domain.com