繁体   English   中英

Send-MailMessage:无法验证参数“主题”上的参数

[英]Send-MailMessage: Cannot validate argument on parameter 'Subject'

运行以下脚本时出现此错误:

Send-MailMessage:无法验证参数“主题”上的参数。 参数为null或为空。 提供一个不为null或为空的参数,然后重试该命令。

电子邮件仍然成功发送,并且主题正确显示。

$dir = "C:\Users\user\Desktop\Lists\TodaysLists"
$SMTPServer = "192.168.1.111"
$Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt')

$japan = @{            
    Name = 'Japan'
    From = "me@me.com
    To   = "you@me.com"
    Cc   = "him@me.com"
}

$ireland = @{            
    Name = 'Ireland'
    From = "me@me.com
    To   = "you@me.com"
    Cc   = "him@me.com"
}

$Regions = @()
$Regions += New-Object PSObject -Property $japan
$Regions += New-Object PSObject -Property $ireland

foreach ($Region in $Regions) {
    $Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse
    $AttachmentName = $Attachment.BaseName
    $Subject = "$AttachmentName"
    $Body = "Please find attached the Report for $($Region.Name).

Produced @ $Time 

Regards,
John Doe
"
    Send-MailMessage -From $Region.From -To $Region.To -CC $Region.Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName
    $Attachment | Move-Item -Destination "C:\Users\user\Desktop\Lists\oldLists"
}

我的猜测是$Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse没有返回一个或多个区域的任何文件,因此$Subject最终是$null

您可以检查该状态并发出警告而不是尝试发送邮件,或者解决该错误(并发送一封电子邮件但主题为空)的另一种方法是添加其他(保证的)文本到$subject 例如:

$Subject = "$($Region.Name): $AttachmentName"

尽管那时我怀疑它会抱怨-Attachments为空。

要添加检查/抛出警告,您可以执行以下操作:

foreach ($Region in $Regions) {
    $Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse

    If ($Attachment) {

        $AttachmentName = $Attachment.BaseName
        $Subject = "$AttachmentName"
        $Body = "Please find attached the Report for $($Region.Name).

    Produced @ $Time 

    Regards,
    John Doe
    "
        Send-MailMessage -From $Region.From -To $Region.To -CC $Region.Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName
        $Attachment | Move-Item -Destination "C:\Users\user\Desktop\Lists\oldLists"
    } Else {
        Write-Warning "One or more files named $($Region.Name) were not found in $dir. Mail not sent."
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM