Powershell已应用于Outlook Com对象-多个筛选器
原职: 目标:每天从一个来源向一个服务帐户发送一封电子邮件。 把它给我的csv文件放在一个文件夹里,交给DBA 没有进一步的告别,以下是我当前的脚本:Powershell已应用于Outlook Com对象-多个筛选器,powershell,outlook,automation,Powershell,Outlook,Automation,原职: 目标:每天从一个来源向一个服务帐户发送一封电子邮件。 把它给我的csv文件放在一个文件夹里,交给DBA 没有进一步的告别,以下是我当前的脚本: #define variables $datestamp = (get-date).AddDays(-1).ToString("MMddyyyy") $datestamp = $datestamp.ToString() $path = "C:\MyPath" $dest = "C:\MyPath\Archive" $file = "MyFile
#define variables
$datestamp = (get-date).AddDays(-1).ToString("MMddyyyy")
$datestamp = $datestamp.ToString()
$path = "C:\MyPath"
$dest = "C:\MyPath\Archive"
$file = "MyFile.csv"
#create outlook session
$objOutlook = New-Object -Com Outlook.Application
$inbox = $objOutlook.Session.GetDefaultFolder(6)
$inbox.Items.Restrict("[UnRead] = True" -and "[SenderEmailAddress] = 'SomePlace@SomeDomain.net'") | select -Expand Attachments | %
{
for ($i = $_.Count; $i; $i--)
{
$_.Item($i).SaveAsFile("C:\MyPath\$($_.Item($i).FileName)")
$_.Parent.Unread = $false
}
}
if (Test-Path "C:\MyPath\*.csv")
{
if(((Get-ChildItem C:\MyPath | Measure-Object ).Count) -gt '1' )
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy@domain.com" -To "PoorITGuy@domain.com" -Subject " FAIL" -Body "FAILED. Too many valid items from mailbox.
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "Too many items to get."
}
else
{
Get-ChildItem $path\*.csv | foreach {Copy-Item $_ "C:\MyPath\$"}
Copy-Item C:\MyPath\*.csv "$path\$file"
Copy-Item C:\MyPath\*.csv "$dest\${datestamp}_$file"
if(Test-Path "$dest\$file")
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy@domain.com" -To "PoorITGuy@domain.com" -Subject "some message”
#cleanup - remove all files from base directory, clean mailbox, close out com object.
Remove-Item "$path\*.csv"
$inbox.Items | select | %
{
for ($i = $_.Count; $i; $i--)
{
$_.Item($i).Delete
}
}
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
}
else
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy@domain.com" -To "PoorITGuy@domain.com" -Subject " failure" -Body "File manipulation failure."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "File manipulation failure."
}
}
}
else
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy@domain.com" -To PoorITGuy@domain.com -Subject "FAIL" -Body "No item mailbox."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "No item to get in inbox."
}
什么不起作用:
$inbox.Items.Restrict("[UnRead] = True" -and "[SenderEmailAddress] = 'SomePlace@SomeDomain.net'") | select -Expand Attachments | %
似乎无法使用多个筛选器限制comObject Outlook.Application
我已经对此做了大量的搜索,但找不到一个关于如何最好地执行此任务的答案。但是我希望我的脚本是防垃圾邮件的,所以它需要知道它是从预期的发件人发送的,并且只需要未读(请参阅下面的错误捕获)
此外,我不确定这是否有效:
$inbox.Items | select | %
{
for ($i = $_.Count; $i; $i--)
{
$_.Item($i).Delete
}
}
还希望对脚本本身进行输入。任何能提高效率的输入都将不胜感激。正确的代码(上面的大量语法错误阻止了它做我想做的事情)
注意-我有一些代码过量
Get-ChildItem $path\*.csv | foreach {Copy-Item $_ "C:\MyPath\$"}
是不正确的语法,并且没有必要,因为脚本已经知道它只是在操作一个文件
要删除收件箱,我需要使用导出邮箱,这似乎是我不想做的;因为执行此功能的服务帐户不是exchange管理员,我也不希望它是exchange管理员。我只是想定期手动删除收件箱,或者让exchange人员自行删除。我修复了大量语法错误。这导致脚本无法执行我想要的操作。它现在可以工作了,除了最后一点。
Get-ChildItem $path\*.csv | foreach {Copy-Item $_ "C:\MyPath\$"}