使用Powershell从powershell发送带附件的电子邮件

时间:2015-02-02 20:17:06

标签: powershell-v2.0

我是Powershell的新手,目前正在寻找一种发送带附件的电子邮件的方法。我尝试使用以下脚本,但是在我给出错误消息(在脚本末尾列出)时失败,请建议我做错了以及如何修复它。

-----------Start -----------
$SourceDir = "D:\powershell testing\*.csv"
$files= Get-ChildItem "$SourceDir"
# ---------------

if (Test-Path $SourceDir) 
{ if ((Get-Item $file).length -gt 5kb) 
{
Start-Process outlook 
$o = New-Object -com Outlook.Application
          Foreach($file in $files) 
          {       
            # -----Email attributes-----
            $mail = $o.CreateItem(0)
            $mail.importance = 2
            $mail.subject = "Problem with LDAP Feed"
            $mail.body = "Please be advised that there is a problem identified with LDAP feed and it is suggested to visit the Automation box for further diagnosis"
            $mail.To = "gary_singh@abc.org"
            # ----Attachments -------
            Write-Host "Attaching File :- " $file
            $attachment = new-object Net.Mail.Attachment.Add($file)            
            $msg.Attachments.Add($attachment)
          }
          # $o.Quit()
        }
}
-----------End--------------


Error Message : 
==============
Get-Item : Cannot find path 'C:\Windows\system32\error 1.csv' because it does not exist.
At line:7 char:19
+if ((Get-Item <<<<  $file).length -gt 5kb) 
+ CategoryInfo: ObjectNotFound:(C:\Windows\system32\error 1.csv:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId: PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

1 个答案:

答案 0 :(得分:0)

问题在于:if ((Get-Item $file).length -gt 5kb) 你的脚本中没有定义$ file,我猜它有一个旧值(C:\ Windows \ system32 \ error 1.csv)

相关问题