使用PowerShell定位共享邮箱以提取附件

时间:2015-02-16 23:29:09

标签: powershell outlook

我对PowerShell相对较新,我有点难题。我有一个脚本,我的一个朋友帮我修改,通过我的电子邮件查找MSG附件,将它们提取到临时文件夹,从每个文件夹中提取xls附件,并将其转换为csv文件(节省了我疯狂的数量)时间)。它整体上运行得很好,但它定位到我的默认收件箱,我想修改它以定位共享邮箱。

这是当前的展望电话;

$olFolderInbox = 6 
$outlook = new-object -com outlook.application; 
$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder($olFolderInbox) 
$messages = $inbox.items

我改编自Sukhija Vikas Here(https://gallery.technet.microsoft.com/office/Outlook-Automation-using-d7584688

我想进一步修改它以查看特定邮箱(例如:reports@company.com),以便我可以获得比意外发送到主电子邮件地址的邮件更多的邮件。

非常感谢任何帮助!

这是整个脚本供参考;

##################################################################################### 
#             Author: Vikas Sukhija 
#             Date:- 09/12/2013 
#                       Description:- read emailbody,extract attachment & send email  
#            with extracted attachment 
#                       Prerequisites :- Powershell/Outlook 
##################################################################################### 
import-module msgutility 
###############################Logs################################################## 
<#$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
$time = get-date -format t 
#$time = $time.ToString().Replace(":", "-") 
$time = $time.ToString().Replace(" ", "") 

#$log1 = ".\Logs" + "\" + "Processed_" + $date + "_.log" 

#$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt" 

Start-Transcript -Path $logs  

$date1 = get-date 
 #>
#############################outlook Call############################################# 
$olFolderInbox = 6 
$outlook = new-object -com outlook.application; 
$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder($olFolderInbox) 
$messages = $inbox.items 
write-host $messages.count 
$messcount = $messages.count 
#add-content $log1 $date1 
#add-content $log1 "Messages Count: $messcount" 
$countprocessed = 0 
foreach($message in $messages){ 
$msubject = $message.subject 
#add-content $log1 "Messages Subject: $msubject" 
$mBody = $message.body 
#Write-Host $mBody 
$mBodySplit = $mBody -split "Customer Email ID:" 
$toaddress1=$mBodySplit[1] 
$toaddress1 
#add-content $log1 "Vendor Email: $toaddress1" 

###################################Save Invoice####################################### 

$filepath = "c:\temp\" 
if ( $msubject -eq "Open/Closed Reports from iClose")
{
$message.attachments|foreach { 
    Write-Host $_.filename 
    $attr = $_.filename 
    #add-content $log1 "Attachment: $attr" 
    $a = $_.filename 
    If ($a.Contains("msg")) { 
    $_.saveasfile((Join-Path $filepath $a)) 
                             } 
  } 
$attachment = "c:\temp\" + $a 
}


}
set-location C:\Temp
Expand-MsgAttachment *

$xls = Get-ChildItem *.xls
foreach ($item in $xls)
{
ExportWSToCSV -excelFileName $item.FullName
} 

修改

我已将我的Outlook调用改为此;

$olFolderInbox = 6 
$outlook = new-object -com outlook.application; 
$namespace = $outlook.GetNameSpace("MAPI"); 
$recipient = $namespace.CreateRecipient("reports@mainspringservices.com")
$inbox = $namespace.GetSharedDefaultFolder($olFolderInbox) 
$messages = $inbox.items 
write-host $messages.count 
$messcount = $messages.count 
#add-content $log1 $date1 
#add-content $log1 "Messages Count: $messcount" 
$countprocessed = 0 
foreach($message in $messages){ 
$msubject = $message.subject 
#add-content $log1 "Messages Subject: $msubject" 
$mBody = $message.body 
#Write-Host $mBody 
$mBodySplit = $mBody -split "Customer Email ID:" 
$toaddress1=$mBodySplit[1] 
$toaddress1 

但我现在收到这个错误:

Cannot find an overload for "GetSharedDefaultFolder" and the argument count: "1".
At C:\Users\Tyler\Documents\Windowspowershell\Powershell files\get-csvfromemail.ps1:30 char:1
+ $inbox = $namespace.GetSharedDefaultFolder($olFolderInbox)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

0
Load : The term 'Load' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path 
is correct and try again.
At C:\Users\Tyler\Documents\WindowsPowerShell\Modules\msgutility\msgutility.psm1:19 char:9
+         Load application
+         ~~~~
    + CategoryInfo          : ObjectNotFound: (Load:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

1 个答案:

答案 0 :(得分:0)

使用Namespace.CreateRecipient传递名称,调用Recipient.Resolve,将Recipient对象传递给Namespace.GetSharedDefaultFolder。