Send-MailMessage:无法验证参数“主题"的参数 [英] Send-MailMessage: Cannot validate argument on parameter 'Subject'
问题描述
运行以下脚本时出现此错误:
This error appears when running the script below:
Send-MailMessage:无法验证参数主题"的参数.参数为 null 或为空.提供一个非 null 或空的参数,然后重试该命令.
Send-MailMessage : Cannot validate argument on parameter 'Subject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
电子邮件仍然发送成功并且主题显示正确.
The email still sends successfully and the subject appears correctly.
$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
.
My guess would be that $Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse
is not returning any files for one or more of the regions, so $Subject
ends up being $null
.
您可以检查该状态并可能抛出警告而不是尝试发送邮件,或者另一种解决错误的方法(并发送电子邮件但主题为空白)是添加其他一些(保证)文本到 $subject
.例如:
You could either check for that state and perhaps throw a warning instead of attempting to send the mail, or another way to work around the error (and get an email sent but with a blank subject) would be to add some other (guaranteed) text to $subject
. E.g:
$Subject = "$($Region.Name): $AttachmentName"
尽管后来我怀疑它会抱怨 -Attachments
为空.
Although then I suspect it would complain about -Attachments
being null.
要添加检查/抛出警告,您可以执行以下操作:
To add a check/throw warning, you could do the following:
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."
}
}
这篇关于Send-MailMessage:无法验证参数“主题"的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!