2014-02-27 55 views
1

我试图将文件附件与特定名称从共享邮箱中的文件夹传输到我的计算机上的目录。从Outlook读取文件附件 - 从共享邮箱

我的脚本如下:

#file path 
$filepath = “c:\test” 
$account = "[email protected]" 


#date string to search for in attachment name 
$date = Get-Date -Format yyyyMMdd 


#set outlook to open 
$o = New-Object -comobject outlook.application 
$n = $o.GetNamespace(“MAPI”) 

$Account = $n.Folders | ? { $_.Name -eq $account }; 
    $f = $Account.Folders | ? { $_.Name -match 'Folder Containing Target Files' }; 



#now loop through them and grab the attachments 
$f.Items | foreach { 
    $_.attachments | foreach { 
    Write-Host $_.filename 
    $a = $_.filename 
    If ($a.Contains($date)) { 
    $_.saveasfile((Join-Path $filepath $a)) 
     } 
    } 
} 

我收到以下错误:You cannot call a method on a null-valued expression.

现在,当我从脚本删除以下块,也不会产生一个错误,但它也不会记录任何文件名。这是错误的,因为子确实有包含满足$date值的文件的电子邮件:

$a = $_.filename 
     If ($a.Contains($date)) { 
     $_.saveasfile((Join-Path $filepath $a)) 
      } 

这让我觉得,我没有成功连接到该邮箱。

在脚本中是否需要更改某些内容,以便我能够成功从该共享邮箱传输文件?我已经读/写/删除邮箱的权限。

回答

0

我重新启动了Outlook,上面的脚本现在可以工作。我认为这是因为我为今天的共享邮箱中的附件传入电子邮件创建了一个文件夹,并且在Outlook重新打开之前,新文件夹在某种程度上没有注册。