【问题标题】:Send-Mailmessage timing out发送邮件超时
【发布时间】:2018-08-09 08:44:46
【问题描述】:

我希望改进旧的批处理脚本并将其升级到 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

任何帮助将不胜感激。

【问题讨论】:

  • 快速编辑:我设法让命令在没有密码的情况下工作(我必须手动输入)Send-MailMessage -SmtpServer mailserver.domain.com - From backupusr@domain.com -To administrator@domain.com - Subject testing -Body "Robocopy results are attached." - DeliveryNotificationOption onFailure -Credential "backupusr@domain.com"我现在的问题是我使用什么语法来添加密码

标签: powershell


【解决方案1】:

-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

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 2016-11-20
    • 1970-01-01
    • 2015-11-25
    • 2016-02-03
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多