Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 发送邮件失败,因为缺少附件(变量出错)_Variables_Powershell - Fatal编程技术网

Variables 发送邮件失败,因为缺少附件(变量出错)

Variables 发送邮件失败,因为缺少附件(变量出错),variables,powershell,Variables,Powershell,我正在用Send MailMessage 有些文件存在,有些文件不存在,因此如果其中一个文件丢失,Send MailMessage将失败 如果文件丢失,是否有办法执行测试路径并清理$attachments 我的代码: $Attachments = "$longlist_file","$PingList_File","$quicklist_file", ` "$nConf_import_host_file","$nConf_import_service_file", ` "$nconf_e

我正在用
Send MailMessage
有些文件存在,有些文件不存在,因此如果其中一个文件丢失,
Send MailMessage
将失败

如果文件丢失,是否有办法执行
测试路径
并清理
$attachments

我的代码:

$Attachments = "$longlist_file","$PingList_File","$quicklist_file", `
  "$nConf_import_host_file","$nConf_import_service_file", `
  "$nconf_export_host_file","$nconf_export_service_file"

# Sending mail
send-mailmessage -to $ToAddress -from $FromAddress -smtpserver $SMTPServer `
  -subject $MessageSubject -Body $MessageBody -Attachments $Attachments

创建仅包含现有文件的新列表:

$realFiles = $Attachments | ? {Test-Path -Path $_}
然后发送

send-mailmessage -to $ToAddress -from $FromAddress -smtpserver $SMTPServer -subject $MessageSubject -Body $MessageBody -Attachments $realFiles 

创建仅包含现有文件的新列表:

$realFiles = $Attachments | ? {Test-Path -Path $_}
然后发送

send-mailmessage -to $ToAddress -from $FromAddress -smtpserver $SMTPServer -subject $MessageSubject -Body $MessageBody -Attachments $realFiles 

像这样的事情应该可以做到:

$Attachments = @("$longlist_file","$PingList_File","$quicklist_file","$nConf_import_host_file","$nConf_import_service_file","$nconf_export_host_file","$nconf_export_service_file")
$TestedAttachments = @()
foreach($file in $Attachments)
{
    try
    {
        $file = get-content -path $file
        write-host "File $($file) exists, adding to tested list"
        $TestedAttachments += $file
    }
    catch[system. exception]
    {
        write-host "File $(file) was not found."
    }
}
send-mailmessage -to $ToAddress -from $FromAddress -smtpserver $SMTPServer -subject $MessageSubject -Body $MessageBody -Attachments $TestedAttachments
问候
Arcass

像这样的东西应该可以做到:

$Attachments = @("$longlist_file","$PingList_File","$quicklist_file","$nConf_import_host_file","$nConf_import_service_file","$nconf_export_host_file","$nconf_export_service_file")
$TestedAttachments = @()
foreach($file in $Attachments)
{
    try
    {
        $file = get-content -path $file
        write-host "File $($file) exists, adding to tested list"
        $TestedAttachments += $file
    }
    catch[system. exception]
    {
        write-host "File $(file) was not found."
    }
}
send-mailmessage -to $ToAddress -from $FromAddress -smtpserver $SMTPServer -subject $MessageSubject -Body $MessageBody -Attachments $TestedAttachments
问候 阿卡斯可能

$Attachments = $longlist_file","$PingList_File","$quicklist_file", `
  "$nConf_import_host_file", "$nConf_import_service_file", `
  "$nconf_export_host_file", "$nconf_export_service_file" `
  | Where-Object { Test-Path $_ }
大概

$Attachments = $longlist_file","$PingList_File","$quicklist_file", `
  "$nConf_import_host_file", "$nConf_import_service_file", `
  "$nconf_export_host_file", "$nconf_export_service_file" `
  | Where-Object { Test-Path $_ }

杰出的谢谢大家的帮助。我投票选择这个作为答案,因为它是如此的简短和紧凑。太棒了!谢谢大家的帮助。我之所以选择这个作为答案,是因为它是如此的简短和紧凑。当你只想测试文件是否存在时,阅读文件是不必要的。您可以使用
Test Path
cmdlet来验证文件是否存在。如果只想测试文件是否存在,则无需读取该文件。您可以使用
测试路径
cmdlet来验证文件是否存在。