【问题标题】:Targeting shared mailbox with powershell to extract attachments使用 PowerShell 定位共享邮箱以提取附件
【发布时间】:2016-09-29 00:59:11
【问题描述】:

我对 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
} 

编辑

我已将我的前景电话改为此;

$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

【问题讨论】:

    标签: powershell outlook


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复德米特里!就像我说的那样,我对所有这些都比较陌生。我会添加这些字符串以使其通过,还是需要散列/删除部分原始调用?
    • 我不确定我理解你的意思。什么弦?您的代码正在使用 GetDefaultFolder,您需要使用 GetSharedDefaultFolder,此外还需要一个 Recipient 对象,您可以从 Namespace.CreateRecipient 获取该对象
    • 我用这些术语把自己绊倒了。我把代码改成了这个;
    • 你改成什么了?
    • 我在原帖中添加了 EDIT 下
    猜你喜欢
    • 2022-11-15
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2021-03-10
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多