variables - 发送邮件消息失败,因为附件丢失(变量变乱)

标签 variables powershell

我正在使用Send-MailMessage发送一堆文件作为附件
有些文件存在,有些不存在,因此,如果缺少其中一个文件,则Send-MailMessage失败。

如果文件丢失,有什么办法可以处理Test-Path并清理$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

最佳答案

大概

$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 $_ }

关于variables - 发送邮件消息失败,因为附件丢失(变量变乱),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8934099/

相关文章:

java - 字符串未在条件中声明?

java - 检测字符串变量中的数字(在 Java 中)

windows - midl 找不到 C 预处理器 cl.exe

powershell - 如何使用 powershell 列出 azure 订阅中所有正在运行的 webjob

php - 如何从 php 中的静态函数内部为公共(public)变量赋值?

java - 如何从不同的类初始化对象并维护状态

linux - Awk 命令行将参数编写为变量以搜索特定字段

regex - $matches ["x"] 在 ISE 中工作,但在命令行 shell 中不起作用

Powershell 并排对象

windows - Active Directory 中是否存在任何主键(或)唯一键?