使用powershell以共享邮箱为目标提取附件
我对powershell比较陌生,我有一个难题。我有一个脚本,我的一个朋友帮我修改,它通过我的电子邮件查找MSG附件,将它们提取到临时文件夹,从每个文件夹提取xls附件,并将其转换为csv文件,为我节省了大量的时间。总体来说,它运行得非常好,但它的目标是我的默认收件箱,我想将其修改为以共享邮箱为目标 这是当前的outlook调用使用powershell以共享邮箱为目标提取附件,powershell,outlook,Powershell,Outlook,我对powershell比较陌生,我有一个难题。我有一个脚本,我的一个朋友帮我修改,它通过我的电子邮件查找MSG附件,将它们提取到临时文件夹,从每个文件夹提取xls附件,并将其转换为csv文件,为我节省了大量的时间。总体来说,它运行得非常好,但它的目标是我的默认收件箱,我想将其修改为以共享邮箱为目标 这是当前的outlook调用 $olFolderInbox = 6 $outlook = new-object -com outlook.application; $ns = $outlook.
$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$messages = $inbox.items
这是我从Sukhija Vikas这里改编的
我想进一步修改它以查看特定的邮箱,例如: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
}
但我现在得到了这个错误:
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
使用Namespace.CreateRecipient传递名称,调用Recipient.Resolve,将收件人对象传递到Namespace.GetSharedDefaultFolder。感谢您的回复!就像我说的,我对这一切都比较陌生。我是要添加这些字符串以使其通过,还是需要散列/删除原始调用的一部分?我不确定我是否理解你的意思。什么弦?您的代码使用的是GetDefaultFolder,您需要使用GetSharedDefaultFolder,这还需要一个收件人对象,您可以从名称空间中获取它。我把代码改成了这个;你把它改成了什么?我把它添加到了“编辑”下的原始帖子中
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