【问题标题】:Send-MailMessage : Cannot validate argument on parameter 'Subject'. [duplicate]Send-MailMessage:无法验证参数“主题”的参数。 [复制]
【发布时间】:2017-05-12 11:42:25
【问题描述】:

电子邮件不会发送。我需要某种 Try /Catch 循环吗?在 Powershell 中检查错误的最佳方法是什么?

我需要它来检查每个位置,如果没有找到一个位置,它仍然会发送其他位置并显示一条消息,说明哪些报告已发送,哪些不取决于是否在文件夹中找到报告

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

   #Defines Directory
   $dir = "C:\Users\user\Desktop\reprts\Todays"
   #Sets STMP server
   $SMTPServer = "10.0.0.46"
   #Declares todays time and formats
   $Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt')

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

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

   $spain = @{            
Name = 'spain'
From = "me@me.com"
To = "you@you.com"
Cc = "her@her.com"
   }


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


   ForEach ($_Region in $_Regions) {

       #Searches dir for list , formats 
       $Attachment = Get-ChildItem -Path $dir -Filter "*$($_Region.name)*"               -Recurse
       $AttachmentName = $Attachment.BaseName

       $Subject = "$AttachmentName"
       $Body = "Please find attached the Missing Image Report for        $($_Region.name).

       Produced @ $Time 

       Regards,
       John Doe
       "
       #Actions Email
       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\reprts\old"

}

【问题讨论】:

  • 您报告的错误消息告诉您问题所在。您需要进行一些调试;您是否尝试检查导致分配给$Subject 的过程的每个步骤的相关值?
  • 我会猜测(我必须这样做,因为它们不是显示您对 Send-MailMessage 的调用的代码)您没有添加主题,或者如果您有它是空的。该错误对于问题是绝对正确的。在您显示的代码中没有$subject
  • 你已经问了两次这个问题,你的第一个更好。如果您仍有问题,请使用相关信息编辑第一个问题。您可以在那里 ping 答案的作者,看看他们是否可以再次帮助您。请避免转发问题。

标签: windows powershell email


【解决方案1】:

我添加了一些错误处理。

如果发送成功与否,将返回区域对象,但会返回一条消息,说明失败的位置/原因

#Defines Directory
$dir = "C:\Users\user\Desktop\reprts\Todays"
#Sets STMP server
$SMTPServer = "10.0.0.46"
#Declares todays time and formats
$Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt')

$_Regions = @(
    @{            
        Name = 'japan'
        From = "me@me.com"
        To   = "you@you.com"
        Cc   = "him@him.com"
    },
    @{  
        Name = 'ireland'
        From = "me@me.com"
        To   = "you@you.com"
        Cc   = "her@her.com"
    },
    @{            
        Name = 'spain'
        From = "me@me.com"
        To   = "you@you.com"
        Cc   = "her@her.com"
    }
)


ForEach ($_Region in $_Regions) {
    $null = $_Region.add('action','Starting work')
    #Searches dir for list , formats 
    try {
        $Attachment = Get-ChildItem -Path $dir -Filter "*$($_Region.name)*" -Recurse -ErrorAction Stop
        $AttachmentName = $Attachment.BaseName

        $Subject = "$AttachmentName"
        $Body = "Please find attached the Missing Image Report for $($_Region.name).

    Produced @ $Time 

    Regards,
    John Doe
    "
        #Actions Email
        Send-MailMessage -From $_Region.From -To $_Region.To -CC $_Region.Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName -ErrorAction Stop
        $Attachment | Move-Item -Destination "C:\Users\user\Desktop\reprts\old" -ErrorAction Stop
        $_Region.action = 'success'
    }
    catch {
        $_Region.action = $_ #catching the error
    }
    finally {
        $_Region | Select-Object name, action
    }
}

【讨论】:

    猜你喜欢
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2019-06-01
    • 2023-02-23
    • 2021-06-04
    • 2021-01-16
    • 1970-01-01
    相关资源
    最近更新 更多