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来验证文件是否存在。