【问题标题】:Send-MailMessage Argument Null or emptySend-MailMessage 参数 Null 或空
【发布时间】:2020-03-06 13:21:37
【问题描述】:

谁能帮我解决这个问题。

#Mail Configuration
$smtpUser = "email@domain.com"
$smtppass = ConvertTo-SecureString "PasswordAsPlainText" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($smtpuser, $smtppass)
$ToAddress = "toaddress@domain.com"
$FromAddress = "send@domain.com"
$SMTPSERVER = "smtp.office365.com"
$SMTPPORT = "587"

$MailParam = @{
        To = $ToAddress
        From = $FromAddress
        Subject = $subject
        Body = $Mail
        SMTPServer = $SMTPServer
        Port = $SMTPPORT
        Credential = $pscred
        }
#Send Email
$GetChildItem = Get-ChildItem -Path C:\Temp
if ($GetChildItem -ne $Null)
    {
    Write-Host "Backup Success"
    $subject = "$env:COMPUTERNAME SQL Backup Success"
    $Mail = "SQL Backup Succeded on the server, please see the attached report for more details"
    $attachment = $reportfile
    Send-MailMessage @MailParam -BodyAsHtml -usessl
   #Exit
    }
if ($GetChildItem -eq $Null)
    {
    Write-Host "Backup Failed"
    $subject = "$env:COMPUTERNAME SQL Backup Failed"
    $Mail = "SQL Backup Failed on the server, please see the attached report for more details"
    $attachment = $reportfile, $debuglog
    Send-MailMessage @MailParam -usessl -BodyAsHtml
    #Exit
    }

现在我认为上面的代码没有任何问题,我不是专业的编码人员,但我几乎可以复制和粘贴 :) 并从那里获取内容以使其为我工作。

上述代码的问题是,第一次运行时,它不起作用,但再次运行时,它运行良好。你第一次得到的错误是

Send-MailMessage:无法验证参数“正文”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。 在 C:\Users\Aasim\Desktop\Untitled1.ps1:29 char:26 + 发送邮件消息@MailParam -BodyAsHtml -usessl + ~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

我不确定问题是什么,以及为什么它只在第一次时不起作用,但在您关闭并重新打开脚本之前每隔一段时间都会起作用。

基本上,我正在创建一个 SQL 备份脚本,用于将数据库备份到我们的网络共享,然后无论成功与否都会通过电子邮件发送给我。到目前为止,脚本的其余部分工作正常。

【问题讨论】:

  • 就在发布这个的时候,我意识到当 $MailParam 被调用时,它确实有 $subject 为空,因为 $subject 直到脚本结束才被定义。我该如何解决这个问题?
  • 我想我想通了,而不是在 $MailParam 中传递主题和正文,我只是将我的 Sen-MailMessage 行替换为下面。 Send-MailMessage @MailParam -Subject $Subject -Body $Mail -Attachments $attachment -BodyAsHtml -usessl
  • 如果您认为其他人可能会遇到类似的问题,并且您找到了您认为未来读者可能会从中受益的解决方案,请将其作为答案发布(您可以在 48 小时后self-accept );否则,请考虑删除您的问题。

标签: powershell powershell-4.0


【解决方案1】:

在您的 If 和 Else 块中,不要创建变量 $subject$Mail,而是更新哈希表 $MailParam。另外,您的 Send-MailMessage 中似乎缺少 Attachment 变量

if ($GetChildItem -eq $Null)
    {
    Write-Host "Backup Failed"
    $MailParam.Subject = "$env:COMPUTERNAME SQL Backup Failed"
    $MailParam.Body = "SQL Backup Failed on the server, please see the attached report for more details"
    $attachment = $reportfile, $debuglog
    Send-MailMessage @MailParam -usessl -BodyAsHtml -Attachments $attachment
    #Exit
    }

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多